8 ms·
You can write literally any JavaScript program with TypeScript (falling back to the `any` type if you really need to), so this doesn't really work in this case.
by nicoburns 8y ago
You can write literally any JavaScript program with TypeScript (falling back to the `any` type if you really need to), so this doesn't really work in this case.
Also, typical TypeScript programs are faster than typical JavaScript programs, because JavaScript JITs like predictable object shapes and monomorphic functions for the same reasons that other language implementations require them. Libraries like lodash and bluebird take advantage of this fact without using TypeScript, but TypeScript steers you towards these patterns.
- snek 8y ago>typical TypeScript programs are faster than typical JavaScript programs Person who works on js engines here :) You would think this is the case, but in actuality, js and ts are about on par in performance (assuming similarly written code). This is in fact due to the fact engines optimize for idiomatic js patterns, not idiomatic typescript patterns. often these will align, but in some cases (usually revolving around generics and inheritance) well written js will actually fall through the optimization pipeline faster due to following more patterns that have specific optimization checks.
- truncate 8y agoAny articles/resources on writing high-performance JS that you would recommend? I don't use JS for work, but just for random fun stuff. So just curious to learn more about it.
- snek 8y agoJust write idiomatic code, it's what all the major engines try to optimize for.
- dahart 8y agoCheck out gl-matrix.js, that’s one designed for being fast. It’s a simple math library, but the main way it achieves fast is by not allocating memory. After having worked on JS for some large web apps that need good graphics performance, the two rules of thumb in my head for making JS fast are: 1- avoid dynamic memory allocation, and 2- avoid using the functional primitives like map. The first one is more or less true in all languages, memory allocation always costs a lot, and if you can pre-allocate memory and/or re-use memory along the way, the code will run faster. This means paying attention to what things in JavaScript will allocate memory under the hood, use of dicts, use of 3rd party library and framework functions, etc. The second one is a bummer; I love using the functional primitives. But map is slower than a for loop, all else being equal. It has gotten relatively faster over time. I mostly use functional everywhere, and only resort to for loops in performance critical code.
- javitury 8y ago> 2- avoid using the functional primitives like map I understand that map requires creating a new array, and that is already included in point 1. What overhead are functional primitives subject to apart from memory allocation? e.g. forEach
- dahart 8y agoUsing continuations. The function call itself and the local scope wrapped up into it can be varying degrees of expensive. When little state, because it’s inline or nearby for example, it might be optimized down to near what a for loop does. But if there’s a function call at all once it’s executed, that alone is slower than the for loop. Remember it’s a function call per element. If the function is further away with more scope, it can be more expensive both memory wise and time wise. BTW, it's easy to test the basic primitives. I use Chrome snippets. test = (name, fn) => { const timeLimitMs = 1000 let start = Date.now(), count = 0 while (Date.now() - start < timeLimitMs) { fn(); count++ } console.log(name, count) } var N = 1000000 let a = new Array(N) test('for loop', _=> { for (var i = 0; i < N; i++) a[i] = i }) test('map', _=> { a = a.map((x,i) => i) }) test('forEach', _=> { a.forEach((v,i,a) => a[i] = i) }) for loop 941 map 37 forEach 69 This is on my Mac in Chrome. So forEach is faster than map, but for loop is more than 10x faster than forEach. That's for loops with trivial work, of course. If the inside of the loop is expensive, the loop/map ratio will be lower.
- nicoburns 8y agoIf I modify that code to pre-initialise an array with the values 0 to N - 1, and then copy the value from that array to a new array (rather than using the loop index), then both map and forEach are faster than the for loop for me.
- dahart 8y agoThat sounds fairly surprising, considering map has to allocate, and allocate is very expensive, but please share your code & I’ll try it. Like so? const N = 1000000 let a = new Array(N), b = new Array(N) for (let i = 0; i < N; i++) a[i] = i test('copy loop', _=> { for (var i = 0; i < N; i++) b[i] = a[i] }) test('copy map', _=> { b = a.map((x,i) => a[i]) }) test('copy forEach', _=> { a.forEach((v,i,a) => b[i] = a[i]) }) I get: copy loop 973, copy map 38, copy forEach 49. Same as before, but this time I tried Chrome in Windows. Are you using a different browser? I know that sometimes other browsers have very different results. In any case, it's somewhat irrelevant if there are cases that optimize and cases that don't. When idiomatic functional code is sometimes up to 30x slower than a for loop, it can't be used in perf critical sections. Even if it's only Chrome and only certain cases. The forEach perf needs to be always reliably performant before I can use it without worry.
- ma2rten 8y agoIs that changing as ts is becoming more popular? I work at Google and I think typescript is picking up quite a lot of momentum inside of Google.
- dahart 8y agoSo why is the TypeScript bench slower?
- hermitdev 8y agoMy best guess: probably written by someone that doeant know how to write performance TS. I say this with no skin in the game. I write neither JS nor TS. What I have observed in language benchmarks over the years, is that the benchmarks are rarely written by an expert, but usually by someone with cursory knowledge of the language. E.g. just enough to be dangerous. Often times, these sorts of benchmarks are done with prejudice (not necessarily malice). The benchmarks are written by someone with something to prove: my chosen tech stack performs better, and let me show you why. A favorite of mine is Perl vs Python comparisons, where you see an idiomatic Perl implementation vs a non idiomatic Python implementation (or other way around). Typically in a head-to-head comparison, the benchmarks are developed by the same individual whom likely has above average knowledge in their favorite and below average in the target they're trying to show as inferior. You'll see this time and time again in internet benchmarks comparing performance. Unless you can see the code from all benchmarks involved, my suggestion is to avoid them. I mean, for all I know, the author of the benchmark was unaware of the built in sort and instead bubble sorted.
- hopler 8y agoI thought the benchmark game is set up so every language's advocates can tune their language's programs. The only chance at a fair comparison is if every language gets the best implementation it can find for the challenges. There's nothing else that approaches fair comparison of apples and oranges.
- dahart 8y agoThis is unfortunately pure speculation on top of pure speculation, which is the problem I have with the top comment. You’re assuming incompetence when you could just go look it up. Why assume it’s someone who doesn’t know? Why use that to wander off into rant land about prejudices and make broad claims that internet benchmarks are bad, when you admit to having zero idea what the actual specific problem here is? The test that lowered TypeScript’s score in the paper is called fannkuch-redux, and here are the sources in question: https://github.com/greensoftwarelab/Energy-Languages/blob/master/JavaScript/fannkuch-redux/fannkuchredux.node-4.node https://github.com/greensoftwarelab/Energy-Languages/blob/ma... https://github.com/greensoftwarelab/Energy-Languages/blob/master/TypeScript/fannkuch-redux/fannkuchredux.ts https://github.com/greensoftwarelab/Energy-Languages/blob/ma... They are both contributed by the same person, and there is no bubble sort involved. So now you know. I don’t see an obvious reason one would be slower, but they’re also quite different. Maybe the algorithmic complexity is different. Maybe the cross-compilation is doing something bad with memory allocation. Note the input sizes for this test are very small, it would be easy for a difference in temporary variables the compiler injects to cause a serious problem. What is not obvious is any prejudice, malice, or incompetence.