jsPerf.app is an online JavaScript performance benchmark test runner & jsperf.com mirror. It is a complete rewrite in homage to the once excellent jsperf.com now with hopefully a more modern & maintainable codebase.
jsperf.com URLs are mirrored at the same path, e.g:
https://jsperf.com/negative-modulo/2
Can be accessed at:
https://jsperf.app/negative-modulo/2
Compare array shift with RapidQueue shift
function createQueue(intialCapacity) {
var that = {};
var head = 0;
var tail = 0;
var length = 0;
var initialCapacity = initialCapacity;
var currentSize = (typeof initialCapacity === undefined) ? initialCapacity : 200;
var container = [];
container.length=currentSize;
function doubling() {
var currentSource = head;
var currentTarget = 0;
var newContainer = [];
newContainer.length = 2*currentSize;
while (currentTarget < currentSize) {
newContainer[currentTarget] = container[currentSource];
currentSource++;
currentTarget++;
if (currentSource == currentSize) {
currentSource = 0;
}
}
container = newContainer;
head = 0;
tail = currentSize;
currentSize *= 2;
}
function shrink() {
var currentSource = head;
var currentTarget = 0;
var newContainer = [];
newContainer.length = currentSize/4;
while (currentTarget < currentSize) {
newContainer[currentTarget] = container[currentSource];
currentSource++;
currentTarget++;
if (currentSource == currentSize) {
currentSource = 0;
}
}
container = newContainer;
head = 0;
tail = currentSize;
currentSize /= 4;
}
that.push = function(element) {
if (length == currentSize) {
doubling();
}
container[tail] = element;
length++;
tail++;
if (tail == currentSize) {
tail = 0;
}
};
that.shift = function() {
if (length === 0) {
return null;
}
tmp = container[head];
head++;
length--;
if (head == currentSize) {
head = 0;
}
if (length == currentSize/4 && length > initialCapacity) {
shrink();
}
return tmp;
};
that.front = function() {
if (length === 0) {
throw new EmptyQueueException();
}
return container[head];
};
that.length = function() {
return length;
};
that.isEmpty = function() {
return length === 0;
};
return that;
}
function EmptyQueueException () {
this.message = "Operation cannot be done because queue is empty";
this.name = "EmptyQueueException";
}
var queue1 = createQueue();
var queue2 = [];
for (var i = 0;i<1000000;i++) {
queue1.push(i);
queue2.push(i);
}
Ready to run.
Test | Ops/sec | |
---|---|---|
RapidQueue.shift() |
| ready |
Array.shift() |
| ready |
You can edit these tests or add more tests to this page by appending /edit to the URL.