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 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36
Browser:
Chrome 121
Operating system:
Mac OS X 10.15.7
Device Platform:
Desktop
Date tested:
2 years ago
Test name
Executions per second
selectionToggleUsingIndexOfSliceConcat
6580.6 Ops/sec
selectionToggleUsingFilter
6057.8 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);