You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
<p>For 90% of the code you write, the bottom line is that performance does not
180
+
matter. For example, if you have some code that reads configuration on startup
181
+
and dumps it into an object, that code might be complicated, but it won't matter
182
+
if it runs in 10 milliseconds or 100 nanoseconds. Write clear code first and
183
+
optimize once things are working. Follow this process, and you will quickly
184
+
figure out which things do and don't matter.</p>
185
+
<h2id="the-cost-of-iteration"><aclass="header" href="#the-cost-of-iteration">The Cost of Iteration</a></h2>
186
+
<p>We use iteration everywhere. Doing it wrong can kill your performance. Doing it
187
+
right can get you close to (single threaded) C performance. This is a quick
188
+
summary of what you can expect. To keep it short, I am just going to cover the
189
+
high points and not show my work.</p>
190
+
<p>The fastest code you can write in pure JavaScript looks like
191
+
<ahref="https://en.wikipedia.org/wiki/Asm.js">asm.js</a>. If you stick to <code>for</code> loops that
192
+
count and index simple types or data object lookups in arrays or numbers in
193
+
typed-arrays (like <code>Uint8Array</code>), you can expect that code to run at or near
194
+
single-threaded C speed.</p>
195
+
<p>Expect <code>for...of</code> with iterables and generators to be about 10x slower. This
196
+
includes array methods like <code>map</code>, <code>filter</code>, and <code>reduce</code>. Anything that has to
197
+
call a function in a loop is going to have extra overhead.</p>
198
+
<p>Promise-driven asynchronous code is another 10x slower, or 100x slower than the
199
+
<code>asm.js</code>-style code. This affects code written using <code>proc</code>, particularly
200
+
<code>Enumerable</code>.</p>
201
+
<p>So does this mean you have to always use <code>asm.js</code> syntax? Not at all. <code>for...of</code>
202
+
syntax and array methods make for cleaner code, and asynchronous operations are
203
+
the whole reason we're here. Iteration performance is mostly about the inner
204
+
loops. If your inner loops are tight, a little less efficiency in the outer
205
+
loops won't matter much. Write clean code first. When things are working, look
206
+
for opportunities to make it faster. Often this will mean a little profiling and
207
+
rewriting a few routines in <code>asm.js</code> style. If you do it right, you should be
208
+
able to get very good performance along with readable code.</p>
216
209
<p><ahref="https://medium.com/netscape/async-iterators-these-promises-are-killing-my-performance-4767df03d85b">Async Iterators: These Promises Are Killing My Performance!</a>
217
210
on Medium and supporting benchmarks in
218
211
<ahref="https://github.com/danvk/async-iteration">async-iteration</a> on Github.</p>
0 commit comments