This short article shows a few methods and compares how they perform when provided with arrays of different sizes.
Arrays containing 25 integers.
Arrays containing 40 thousand integers.
Arrays containing 4 million integers.
Arrays containing 4 integers.
- Performance wise the clear winner is
Regular for loop with object key.
- In terms of conciseness and readability,
Spread setis my favorite.
- Not suprisingly
Filter with indexOfperforms well with tiny arrays, where it’s O(n²) is not an issue.
Spread setwas 76% slower for larger arrays, even though it was the second best. I would have thought it would be the most optimized solution.