object.keys vs push

JavaScript performance comparison

Test case created

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var a = []
    for (var i = 0; i < 10; i++) {
      a[i] = Math.floor(Math.random() * 5)
    }
};
</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
keys
var i = a.length,
    hash = Object.create(null)
while (i--) {
   var key = a[i]
   if (hash[key]) continue
   hash[key] = 1
}
b = Object.keys(hash)
pending…
push
var i = a.length,
    b = [],
    hash = Object.create(null)
while (i--) {
   var key = a[i]
   if (hash[key]) continue
   hash[key] = 1
   b.push(key)
}
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