Splice vs Map.delete

JavaScript performance comparison

Test case created by Edmundo Rodrigues

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
    var spliceRemove = function(array, el) {
        var index = array.indexOf(el);
  
        if (index === -1) return;
  
        array.splice(index, 1);
    };
  
    var mapRemove = function(map, el) {
        if (!map.has(el)) return;
  
        map.delete(el);
    }
    
    longArray = Array(10000);
    longMap = new Map();
    
        var i=longArray.length;
        while (i--) {
            longArray[i] = i % 256;
            longMap.set(i, i % 256);
        }
   
    

};

Benchmark.prototype.teardown = function() {
  delete longArray;
  delete longMap;

};
</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
splice(el, 1)
for (var i = 0; i < longArray.length; i++) {
spliceRemove(longArray, i);
}
pending…
map.delete
for (var i = 0; i < longMap.entries().length; i++) {
mapRemove(longMap, i);
}
pending…

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

0 Comments