ES6 Set vs Array properties

JavaScript performance comparison

Revision 102 of this test case created by

Preparation code

<script>
function randomString()
{
    var text = "";
    var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";

    for( var i=0; i < 5; i++ )
        text += possible.charAt(Math.floor(Math.random() * possible.length));

    return text;
}
</script>
      
<script>
Benchmark.prototype.setup = function() {
  var set = new Set();
  var arr = [];
  
  var set2 = new Set();
  var arr2 = [];
  
  for (var i=0; i < 1000; i++) {
     var key = randomString();
     set2.add(key);
     arr2.push(key);
  }

};
</script>

Test runner

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

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
set.add(key)
for (var i=0; i < 1000; i++) {
   var key = randomString();
   set.add(key);
}
pending…
arr.push(key);
for (var i=0; i < 1000; i++) {
   var key = randomString();
   arr.push(key);
}
pending…
Iterate Set
for (var key of set2.values()) {
  var val = set2.has(key);
}
pending…
Iterate Array
var key;
for (key in arr2) {
  if (arr2.hasOwnProperty(key)) {
    var val = arr2[key];
  }
}
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.

0 Comments