slice VS splice VS filter: deleting one item
100k list
Date tested:
2 years ago
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36 Edg/96.0.1054.62
Test name
Executions per second
slice
96.0 Ops/sec
splice
580.2 Ops/sec
filter
59.9 Ops/sec
Benchmark definition (click to collapse):
Script Preparation code:
var list = []; for (var i = 0; i < 1000 * 1000; i++) { list.push(i); }
Tests:
slice
list = [ ...list.slice(0, 50000), ...list.slice(50000 + 1) ];
splice
newList = [...list]; newList.splice(50000, 1);
filter
newList = list.filter(i => i !== 50000);
Open this result on MeasureThat.net