microsoft / TypeScript

TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
https://www.typescriptlang.org
Apache License 2.0
99k stars 12.29k forks source link

Design Meeting Notes, 6/18/2024 #58919

Open DanielRosenwasser opened 2 weeks ago

DanielRosenwasser commented 2 weeks ago

Coverage Reporting

https://github.com/microsoft/TypeScript/pull/58850

Knip

https://github.com/microsoft/TypeScript/pull/56817

@internal Tagging

More "Primitive" Operations in Core Utilities

https://github.com/microsoft/TypeScript/pull/58873

fatcerberus commented 1 week ago

for-of loops create iterators but for (let i = 0; i < arr.length; i++) doesn't.

I'd have expected for (const foo of foos) {} to be highly optimized by engines at this point. Since the iterator for arrays doesn't actually do anything, I'd be surprised if most engines didn't optimize it away in this case.

Jamesernator commented 1 week ago

I'd have expected for (const foo of foos) {} to be highly optimized by engines at this point. Since the iterator for arrays doesn't actually do anything, I'd be surprised if most engines didn't optimize it away in this case.

Still not really, a for-of-loop is still often many times slower than a for-loop, e.g. this has a difference of 10x in v8 , and 4x in jsc (curiously inlining the forOfLoop function makes v8 have a 4x difference as well):

const arr = Array.from({ length: 10_000_000 }, () => Math.random());

function forLoop() {
    let sum = 0;
    for (let i = 0; i < arr.length; i += 1) {
        sum += arr[i];
    }
}

function forOfLoop() {
    let sum = 0;
    for (const v of arr) {
        sum += v;
    }
}

console.time("for-loop");
for (let i = 0; i < 100; i += 1) {
    forLoop();
}
console.timeEnd("for-loop");

console.time("for-of-loop");
for (let i = 0; i < 100; i += 1) {
    forOfLoop();
}
console.timeEnd("for-of-loop");