Splicing a single value at middle point (v8)

Revision 8 of this benchmark created by Ted Ying on


Description

Unshift adds values while shift removes them.

Preparation HTML

<script>
  var arr = [];
  for (var i = 0; i < 50000; i++) {
    arr.push(i);
  };
  var half = Math.floor(arr.length / 2);

  function getArr() {
    return arr.slice(0);
  };

  function randomInt(max) {
    return Math.floor(Math.random() * (max + 1));
  };
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
Splice insertion at end
var arr = getArr();
for (var i = 0; i < 1000; i++) {
  arr.splice(half, 0, -1 - i);
}
ready
Push
var arr = getArr();
for (var i = 0; i < 1000; i++) {
  arr.push(-1 - i);
}
ready
Shift
var arr = getArr();
for (var i = 0; i < 1000; i++) {
  arr.unshift(-1 - i);
}
ready

Revisions

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