Yield* vs yield in for loop

JavaScript performance comparison

Test case created by weswigham

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
Yield*
function* gen() {
  for(let i = 0; i < 1e7; i++) {
    yield i;
  }
}

function* gen2() {
  yield -1;
  yield* gen();
}

let acc = 0;
for (const e of gen2()) {
  acc += e;
}
pending…
Manual for loop over generator object
function* gen() {
  for(let i = 0; i < 1e7; i++) {
    yield i;
  }
}

function* gen2() {
  yield -1;
  const generator = gen();
  let res = generator.next();
  for (; !res.done; res = generator.next()) {
    yield res.value;
  }
}

let acc = 0;
for (const e of gen2()) {
  acc += e;
}
pending…
Yield* no const for of
function* gen() {
  for(let i = 0; i < 1e7; i++) {
    yield i;
  }
}

function* gen2() {
  yield -1;
  yield* gen();
}

let acc = 0;
const generator = gen2();
let res = generator.next();
for (; !res.done; res = generator.next()) {
  acc += res.value;
}
pending…
Manual for loop over generator no for-of
function* gen() {
  for(let i = 0; i < 1e7; i++) {
    yield i;
  }
}

function* gen2() {
  yield -1;
  const generator = gen();
  let res = generator.next();
  for (; !res.done; res = generator.next()) {
    yield res.value;
  }
}

let acc = 0;
const generator = gen2();
let res = generator.next();
for (; !res.done; res = generator.next()) {
  acc += res.value;
}
pending…

You can edit these tests or add even more tests to this page by appending /edit to the URL.

0 Comments