https://github.com/danasilver/splice-pop-slice-perf
splice vs pop vs slice to create subsets of an array
https://github.com/danasilver/splice-pop-slice-perf
Last synced: about 1 year ago
JSON representation
splice vs pop vs slice to create subsets of an array
- Host: GitHub
- URL: https://github.com/danasilver/splice-pop-slice-perf
- Owner: danasilver
- Created: 2014-10-09T03:00:37.000Z (over 11 years ago)
- Default Branch: master
- Last Pushed: 2014-10-11T04:20:09.000Z (over 11 years ago)
- Last Synced: 2025-01-06T00:41:55.018Z (over 1 year ago)
- Language: JavaScript
- Homepage:
- Size: 234 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
## slice vs pop vs slice to create subsets of an array
Original array of size 10000.
Small subsets have size 2. Large subsets have size 1000.
Doing multiple (2) pops is the fastest if the subsets are small, but calling pop
1000 times for the large subset becomes a bottleneck. Slice is fastest for a
large subset. Since slice doesn't modify the original array (unlike pop and
splice), after calling `Array.slice`, we set `Array.length -= n` where `n` is
the subset size.
### Results
```
splice (small subset) x 1,712 ops/sec ±0.70% (95 runs sampled)
slice (small subset) x 1,572 ops/sec ±0.52% (96 runs sampled)
multiple pops (small subset) x 2,428 ops/sec ±1.06% (94 runs sampled)
splice (large subset) x 17,063 ops/sec ±0.50% (97 runs sampled)
slice (large subset) x 15,644 ops/sec ±0.63% (95 runs sampled)
multiple pops (large subset) x 7,930 ops/sec ±0.34% (102 runs sampled)
Fastest is splice (large subset)
```

### Summary
Use pop if you only need to remove a few elements from the end of an array; use
slice (or splice) if you need to remove a large number of elements.