array shift vs pop

Benchmark created on


Description

demonstrate shift is not O(1) operation. each test replaces

Setup

const COUNT = 3000;
let arr = new Array(COUNT).fill().map((l, i) => i);

Test runner

Ready to run.

Testing in
TestOps/sec
replacing items in place (ex. circular buffer)
for(let i = 0; i < COUNT; i++) {
  arr[i] = i;
}
ready
shifting and pushing
for(let i = 0; i < COUNT; i++) {
  arr.push(i);
  arr.shift();
}
ready

Revisions

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