distinct

JavaScript performance comparison

Test case created by Dan Milon

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var arr = []
   
    for (var i = 0; i < 50; i++) {
      arr.push({ val: Math.floor(Math.random() * 10) })
    }
};
</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
var a = []

for (var i = 0; i < arr.length; i++) {
  if (a.indexOf(arr[i].val) === -1) {
    a.push(arr[i].val)
  }
}

console.log(a)
pending…
object
var obj = {}

for (var i = 0; i < arr.length; i++) {
  obj[arr[i].val] = 1
}

console.log(Object.keys(obj))
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