Skip to content

Instantly share code, notes, and snippets.

@webstrand
Created November 3, 2023 14:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save webstrand/f1803b637f345e41e95a9bbd0632bcaf to your computer and use it in GitHub Desktop.
Save webstrand/f1803b637f345e41e95a9bbd0632bcaf to your computer and use it in GitHub Desktop.
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