splice vs removeItems

Benchmark created on


Setup

const createArray = () => new Array(1000000).fill(0).map(i => Math.random());

const remove1 = (arr, startIdx, removeCount) => {arr.splice(startIdx, removeCount)};

const remove2 = (arr, startIdx, removeCount) => {
    const length = arr.length;
    if (startIdx >= length || removeCount === 0) {
        return;
    }
    removeCount = (startIdx + removeCount > length ? length - startIdx : removeCount);
    const len = length - removeCount;
    for (let i = startIdx; i < len; ++i) {
        arr[i] = arr[i + removeCount];
    }
    arr.length = len;
};

const runTest = (removeFn) => {
	const arr = createArray();
	for (let i = 0; i < 1000; ++i) {
		const idx = Math.floor(Math.random() * arr.length);
		const count = Math.floor(Math.random() * 100);
		removeFn(arr, idx, count);
	}
};

Test runner

Ready to run.

Testing in
TestOps/sec
splice
runTest(remove1);

ready
removeItems
runTest(remove2);
ready

Revisions

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