Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Run results for:
Reducer Test #001 01.10.2024
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/127.0.0.0 Safari/537.36
Browser:
Chrome 127
Operating system:
Mac OS X 10.15.7
Device Platform:
Desktop
Date tested:
one year ago
Test name
Executions per second
forEach Push
1511.5 Ops/sec
reduce
673.1 Ops/sec
HTML Preparation code:
<ul> <li></li> <li></li> <li></li> <li></li> <li></li> <li></li> <li></li> <li></li> <a></a> </ul>
Tests:
forEach Push
const forEachTest = (nthElements, excludeBeforeElements, excludeAfterElements) => { const daiContainers = []; nthElements.forEach((nthElement) => { // Check if we should exclude this element due to the element before / after const nextElement = nthElement.nextElementSibling; // If this nthElement is one we shouldn't come AFTER // or the NEXT element is the one we shouldn't come BEFORE // then return (being explicit here for clarity) if ( excludeAfterElements.includes(nthElement) || excludeBeforeElements.includes(nextElement) ) { return; } const daiContainerElement = document.createElement('div'); daiContainerElement.setAttribute('class', 'test-class'); nthElement.after(daiContainerElement); daiContainers.push(daiContainerElement); }); return daiContainers; } const nthElements = Array.from(document.querySelectorAll('ul li')); const excludeBeforeElements = Array.from(document.querySelectorAll('ul a')); const excludeAfterElements = []; const containers = forEachTest(nthElements, excludeBeforeElements, excludeAfterElements);
reduce
const reduceTest = (nthElements, excludeBeforeElements, excludeAfterElements) => { return nthElements.reduce((daiContainers, nthElement) => { const nextElement = nthElement?.nextElementSibling; // Skip insertion if nthElement or its next sibling is excluded if (excludeAfterElements.includes(nthElement) || excludeBeforeElements.includes(nextElement)) { return daiContainers; } // Create and insert ad container const daiContainerElement = document.createElement('div'); daiContainerElement.setAttribute('class', 'test-class'); nthElement.after(daiContainerElement); daiContainers.push(daiContainerElement); return daiContainers; }, []); } const nthElements = Array.from(document.querySelectorAll('ul li')); const excludeBeforeElements = Array.from(document.querySelectorAll('ul a')); const excludeAfterElements = []; const containers = reduceTest(nthElements, excludeBeforeElements, excludeAfterElements);