473,406 Members | 2,352 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Queues and Javascript engines

A long time ago when I used a browser that didn't support the shift or
unshift methods of the array object, I came up with an implementation of
queues that guaranteed amortised constant time dequeuing by only moving
elements in the array when the 'space' at the front of the array was as
long as the queue it represented. In modern Javascript it would look like
this:

function Queue(){
var queue=new Array();
var queueSpace=0;
this.enqueue=function(element){
queue.push(element);
}
this.dequeue=function(){
if (queue.length){
var element=queue[queueSpace];
if (++queueSpace*2 >= queue.length){
for (var i=queueSpace;i<queue.length;i++)
queue[i-queueSpace]=queue[i];
queue.length-=queueSpace;
queueSpace=0;
}
return element;
}else{
return undefined;
}
}
}

An unencapsulated version of this had been sitting on a page on my website
for a few years when someone e-mailed me to ask if I knew how well this
performed in various browsers (I didn't) and if I'd be interested in the
results of some tests he would do (I would). Unfortunately he never
e-mailed back, so recently I did some basic tests on my own comparing it,
in Opera, Firefox, and IE, to a basic implementation using the shift
method, and an implementation using a linked list. The results (complete
with pretty graphs) can be seen at the bottom of this page:

http://www.safalra.com/programming/javascript/queues/

I now have some questions about the results I obtained, and wonder if
anyone here who knows how the browsers' Javascript engines work internally
may be able to help.

Comparing this 'delayed shift queue' (DSQ) to a queue using the shift
method showed the DSQ to be significantly faster for larger queues, which
is to be expected as dequeuing is O(1) rather than O(n). Opera was the
fastest (which makes a change from the days when its Javascript engine was
painfully slow), followed by Firefox and then IE, and the slower the
browser the smaller the queue length for which it showed improvements with
the DSQ.

I was puzzled, however, by the comparison of the DSQ to the linked-list
queue, whose graphs is lacking the smooth curves of the first comparison.
In Opera the linked-list queue was about twice as fast until large queue
lengths (over 100000 elements), at which point performance suddenly
declined dramatically, with the linked-list queue taking twice as long as
the DSQ for 500000 elements. In IE the two implementations had very similar
running times, until about 500000 elements when the linked-list queue toook
three times as long as the DSQ. In Firefox the DSQ was always faster, but
the linked-list queue didn't suffer from the sudden drop in performance
that occured in the other two browsers. I was wondering if any of you had
theories (or even better, firm knowledge) of why the three browsers behave
like this.

--
Safalra (Stephen Morley)
http://www.safalra.com/programming/javascript/
Dec 23 '06 #1
1 1745

"Safalra" <us****@safalra.comwrote in message
news:1v****************************@40tude.net...
>
I was puzzled, however, by the comparison of the DSQ to the linked-list
queue, whose graphs is lacking the smooth curves of the first comparison.
In Opera the linked-list queue was about twice as fast until large queue
lengths (over 100000 elements), at which point performance suddenly
declined dramatically, with the linked-list queue taking twice as long as
the DSQ for 500000 elements. In IE the two implementations had very
similar
running times, until about 500000 elements when the linked-list queue
toook
three times as long as the DSQ. In Firefox the DSQ was always faster, but
the linked-list queue didn't suffer from the sudden drop in performance
that occured in the other two browsers. I was wondering if any of you had
theories (or even better, firm knowledge) of why the three browsers behave
like this.
I would imagine that the way each engine was handling memory (ie; pooling
etc) and the interaction of the subsystem in paging out other memory blocks
to make room would account for much of the observed behaviour.
Repeating the same code, no matter how many times, will not slow down a CPU
to the best of my knowledge. However, using an ever increasing amount of
finite memory in a system that broadens what is physicaly available via
paging is certainly going to show a non linear performance.
Different imlementations will almost certainly allocate more or less memory
for an object, and one may be using pooling, and the other not. In these
cases paging may be handled differently, and any symmetry in the performance
of the two objects would then be lost as paging came into effect.
Well, it's a theory ;)
Vince Morgan

Dec 23 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Anand Pillai | last post by:
The standard Python Queue module, allows to generate queues that have no size limit, by passing the size argument as <= 0. q = Queue(0) In a multithreaded application, these queues could be...
0
by: Frank | last post by:
Hey all, I can't seem to get javascript running in my XSL document. <?xml version="1.0"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"...
31
by: manno | last post by:
Hi all, more or less just out of curiosity... I had a short 'discussion' about JavaScript in different borwsers. The other guy said that there's differeces in JavaScript accross browsers (I...
1
by: Robert Mark Bram | last post by:
Hi All, I am writing a Javascript UI component. I have already written a "disable()" method for it, but I would like to go one step further in order to make my component as compatible with...
1
by: eastcoastguyz | last post by:
I know that with a web site which is simply .html web pages, it is easy for a bot to spider it and follow links. What about those web sites written in .php or Javascript? How does its content get...
1
by: Kwang Yul Seo | last post by:
Hi all, I am looking for JavaScript libraries based on JavaScript Core Library (http://www.webreference.com/javascript/reference/core_ref/ contents.html) without using client-side JavaScript. Is...
10
by: MohsinHijazee | last post by:
Hello there! What if I want to give my desktop application some scripting capabilities by exposing the object model of it. How do we do that? Are their any JavaScript Execution Engines out their?...
30
by: Jeff Bigham | last post by:
So, it appears that Javascript has a recursion limit of about 1000 levels on FF, maybe less/more on other browsers. Should such deep recursion then generally be avoided in Javascript?...
14
by: MartinRinehart | last post by:
I can load a dialog by loading an HTML page or by constructing the dialog with JavaScript. What should I be thinking about when I look at this choice?
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.