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 (iPhone; CPU iPhone OS 17_2 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.2 Mobile/15E148 Safari/604.1
Browser:
Mobile Safari 17
Operating system:
iOS 17.2
Device Platform:
Mobile
Date tested:
2 years ago
Test name
Executions per second
selectionToggleUsingIndexOfSliceConcat
1556.2 Ops/sec
selectionToggleUsingFilter
1547.2 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);