truncating arrays correctly

JavaScript performance comparison

Test case created by Kyle Simpson

Preparation code

<script>var arr, i, len, result;</script>
    

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
length = 0
arr = new Array(5001).join("-").split("");

len = arr.length;

for (i=0; i<len; i=i+50) {
   arr[i] += "x";
}

// here's what we're actually testing
arr.length = 0;
// ----------------------------------

for (i=0; i<1000; i++) {
   arr.push(i*3);
}

result = (arr.length === 1000 && len === 5000);
if (!result) throw "Test broken";
pending…
= [ ]
arr = new Array(5001).join("-").split("");

len = arr.length;

for (i=0; i<len; i=i+50) {
   arr[i] += "x";
}

// here's what we're actually testing
arr = [];
// ----------------------------------

for (i=0; i<1000; i++) {
   arr.push(i*3);
}

result = (arr.length === 1000 && len === 5000);
if (!result) throw "Test broken";
pending…
.shift()
arr = new Array(5001).join("-").split("");

len = arr.length;

for (i=0; i<len; i=i+50) {
   arr[i] += "x";
}

// here's what we're actually testing
for (i=0; i<len; i++) {
   arr.shift();
}
// ----------------------------------

for (i=0; i<1000; i++) {
   arr.push(i*3);
}

result = (arr.length === 1000 && len === 5000);
if (!result) throw "Test broken";
pending…
.pop()
arr = new Array(5001).join("-").split("");

len = arr.length;

for (i=0; i<len; i=i+50) {
   arr[i] += "x";
}

// here's what we're actually testing
for (i=0; i<len; i++) {
   arr.pop();
}
// ----------------------------------

for (i=0; i<1000; i++) {
   arr.push(i*3);
}

result = (arr.length === 1000 && len === 5000);
if (!result) throw "Test broken";
pending…
.splice()
arr = new Array(5001).join("-").split("");

len = arr.length;

for (i=0; i<len; i=i+50) {
   arr[i] += "x";
}

// here's what we're actually testing
arr.splice(0, len);
// ----------------------------------

for (i=0; i<1000; i++) {
   arr.push(i*3);
}

result = (arr.length === 1000 && len === 5000);
if (!result) throw "Test broken";
pending…

Compare results of other browsers

Revisions

You can edit these tests or add even more tests to this page by appending /edit to the URL.

0 Comments