Created
November 3, 2023 16:24
-
-
Save conartist6/843495ca1e8db096ea0fde22e7af46b9 to your computer and use it in GitHub Desktop.
Iterator benchmark
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { IsoBench } from 'iso-bench'; | |
const range = max => ({ | |
*[Symbol.iterator]() { | |
for (let i = 0; i < max; i++) yield i; | |
} | |
}); | |
globalThis.i = 0; | |
const length = 100000; | |
const arr = Array.from({ length }, (_, i) => i); | |
await new IsoBench('for .. of') | |
.add( | |
'range', | |
(iterableIterator) => { | |
for (const value of iterableIterator) { | |
globalThis.i = i ^ value; | |
} | |
}, | |
() => range(length), | |
) | |
.add( | |
'array', | |
(iterableIterator) => { | |
for (const value of iterableIterator) { | |
globalThis.i = i ^ value; | |
} | |
return i; | |
}, | |
() => arr, | |
) | |
.consoleLog() | |
.run(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment