Count undefined array elements (v3)

Revision 3 of this benchmark created by Marcin on


Test runner

Ready to run.

Testing in
TestOps/sec
filter()
var arr = [1,,2,5,6,,4,5,6,,];
var empties = arr.length - arr.filter(function(x){ return true }).length;
ready
forEach
var arr = [1,,2,5,6,,4,5,6,,];
var empties = arr.length;
arr.forEach(function(x){ empties--;  });
ready
map()
var arr = [1,,2,5,6,,4,5,6,,];
var empties = arr.map(function(v,i,a, und){ return v === und;  }).length;
ready
Sort and loop
var arr = [1,,2,5,6,,4,5,6,,], count = 0;
arr.sort();
while (typeof arr.pop() === "undefined") count++;
 
ready
Simple "for" 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++;
}
ready
Simple "while" loop
var arr = [1,,2,5,6,,4,5,6,,], count = 0, i = arr.length;

while (i--) {
    if (typeof arr[i] === "undefined")
        count++;
}
 
ready
Join
var arr = [1,,2,5,6,,4,5,6,,];

(arr.join('') === '');
 
ready

Revisions

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