Filtering defined values in array

JavaScript performance comparison

Test case created by Mourner

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var i, len = 1000;
   
    var array = new Array(len);
   
    for (i = 0; i < len; i += 3) {
       array[i] = 1;
    }
   
    function filterFn(item) {
      return typeof item !== 'undefined';
    }
   
    var result;
   
    var typedArray = new Uint8Array(array);
};
</script>

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
Array.filter
result = array.filter(filterFn);
pending…
for loop
result = [];
for (i = 0; i < len; i++) {
  if (typeof array[i] !== 'undefined') {
    result.push(array[i]);
  }
}
pending…
for in
result = [];
for (i in array) {
  if (array.hasOwnProperty(i)) {
    result.push(array[i]);
  }
}
pending…
for loop typed
result = [];
for (i = 0; i < len; i++) {
  if (typeof typedArray[i] !== 'undefined') {
    result.push(typedArray[i]);
  }
}
pending…
for in typed
result = [];
for (i in typedArray) {
  if (array.hasOwnProperty(i)) {
    result.push(typedArray[i]);
  }
}
pending…

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

Compare results of other browsers

0 comments

Add a comment