Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Run results for:
filter vs slice+concat
Go to the benchmark
Embed
Embed Benchmark Result
Run details:
User agent:
Mozilla/5.0 (X11; Linux x86_64; rv:122.0) Gecko/20100101 Firefox/122.0
Browser:
Firefox 122
Operating system:
Linux
Device Platform:
Desktop
Date tested:
2 years ago
Test name
Executions per second
selectionToggleUsingIndexOfSliceConcat
3444.9 Ops/sec
selectionToggleUsingFilter
3293.6 Ops/sec
Tests:
selectionToggleUsingIndexOfSliceConcat
const selectedAddressGroupIds = Array.from( new Set(new Array(500).fill(null).map(() => Math.floor(Math.random() * 5000))) ); const toggleAddressGroupIds = Array.from( new Set(new Array(50).fill(null).map(() => Math.floor(Math.random() * 5000))) ); const selectionToggleUsingIndexOfSliceConcat = (addressGroupId) => { const selectedIndex = selectedAddressGroupIds.indexOf(addressGroupId); let newSelected = []; // Following will be faster than `Array.filter()` because `Array.indexOf()` & `Array.concat()` combined together is faster if (selectedIndex === -1) { newSelected = newSelected.concat(selectedAddressGroupIds, addressGroupId); } else if (selectedIndex === 0) { newSelected = newSelected.concat(selectedAddressGroupIds.slice(1)); } else if (selectedIndex === selectedAddressGroupIds.length - 1) { newSelected = newSelected.concat(selectedAddressGroupIds.slice(0, -1)); } else if (selectedIndex > 0) { newSelected = newSelected.concat( selectedAddressGroupIds.slice(0, selectedIndex), selectedAddressGroupIds.slice(selectedIndex + 1) ); } return newSelected; }; toggleAddressGroupIds.map(selectionToggleUsingIndexOfSliceConcat);
selectionToggleUsingFilter
const selectedAddressGroupIds = Array.from( new Set(new Array(500).fill(null).map(() => Math.floor(Math.random() * 5000))) ); const toggleAddressGroupIds = Array.from( new Set(new Array(50).fill(null).map(() => Math.floor(Math.random() * 5000))) ); const selectionToggleUsingFilter = (addressGroupId) => { const selectedIndex = selectedAddressGroupIds.indexOf(addressGroupId); let newSelected = []; // Following will be faster than `Array.filter()` because `Array.indexOf()` & `Array.concat()` combined together is faster if (selectedIndex === -1) { newSelected = newSelected.concat(selectedAddressGroupIds, addressGroupId); } else { newSelected = selectedAddressGroupIds.filter((id) => id !== addressGroupId); } return newSelected; }; toggleAddressGroupIds.map(selectionToggleUsingFilter);