Count undefined array elements

JavaScript performance comparison

Test case created by Andy

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
filter()
var arr = [1,,2,5,6,,4,5,6,,];
var empties = arr.length - arr.filter(function(x){ return true }).length;
pending…
forEach
var arr = [1,,2,5,6,,4,5,6,,];
var empties = arr.length;
arr.forEach(function(x){ empties--;  });
pending…
map()
var arr = [1,,2,5,6,,4,5,6,,];
var empties = arr.map(function(v,i,a, und){ return v === und;  }).length;
pending…
Sort and loop
var arr = [1,,2,5,6,,4,5,6,,], count = 0;
arr.sort();
while (typeof arr.pop() === "undefined") count++;
 
pending…
Simple loop
var arr = [1,,2,5,6,,4,5,6,,], count = 0;

for (var i=arr.length - 1; i; i--) {
    if (typeof arr[i] === "undefined")
        count++;
}
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. Here’s a list of current revisions for this page:

0 comments

Add a comment