Created
November 3, 2023 14:58
-
-
Save webstrand/f1803b637f345e41e95a9bbd0632bcaf to your computer and use it in GitHub Desktop.
This file contains 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"; | |
function* createIterable(count) { | |
for (let i = 0; i < count; i++) { | |
yield i; | |
} | |
} | |
declare var i: number; | |
globalThis.i = 0; | |
await new IsoBench("generated iterable") | |
.add("while", (iterable) => { | |
let next = iterable.next(); | |
while (!next.done) { | |
next = iterable.next(); | |
i = i ^ next.value!; | |
} | |
}, () => createIterable(100000)) | |
.add("do while", (iterable) => { | |
let next; | |
do { | |
next = iterable.next(); | |
i = i ^ next.value!; | |
} | |
while(!next.done); | |
}, () => createIterable(100000)) | |
.add("for ... of", (iterable) => { | |
for(const value of iterable) { | |
i = i ^ value; | |
} | |
}, () => createIterable(100000)) | |
.consoleLog().run(); | |
const arr = Array.from({ length: 100000 }, (_, i) => i); | |
await new IsoBench("array iterable") | |
.add("while", (iterable) => { | |
let next = iterable.next(); | |
while (!next.done) { | |
next = iterable.next(); | |
i = i ^ next.value!; | |
} | |
}, () => arr.values()) | |
.add("do while", (iterable) => { | |
let next; | |
do { | |
next = iterable.next(); | |
i = i ^ next.value!; | |
} | |
while(!next.done); | |
}, () => arr.values()) | |
.add("for ... of", (iterable) => { | |
for(const value of iterable) { | |
i = i ^ value; | |
} | |
}, () => arr.values()) | |
.consoleLog().run(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment