JavaScript Performance

These tests time a million of something, in as many of the different ways as I could think of, so you can see how they compare. They run here in your own browser, alongside everything else it's doing, so they aren't scientific, but they give a rough idea of the differences.

Press Run on a row to time one test, or Run all under a table to go through the table from the top; Run all turns into a Stop button, which stops once the test that's running has finished. Each test runs five times, and the result is the average of the five. Anything a test needs is set up again before each run, outside the timing, and each section says what that is.

The result is the time for the whole million and the time per entry. With a million entries, these are the same number: a millisecond in total is a nanosecond per entry.

The M2 Max column is from my own machine, a MacBook Pro with an Apple M2 Max, running every table with Run all in Chrome 156, so you can compare it with yours. The fastest way of doing each job is shown in green and the slowest in red, in my column and in yours as your tests finish. Where a table does more than one job, such as the Map table, which sets, checks, reads and deletes, each test is only compared with the others doing the same job, and each typed array type is compared only with itself.

Some things to bear in mind when reading the numbers:

  • The first of the five runs is often the slowest, while the engine is still working out how best to compile the code, and the average includes it.
  • Browsers round performance.now() to a tenth of a millisecond or more, as a guard against timing attacks, so anything under a millisecond or so is only a rough number.
  • The page stops responding while a test is running, and some of them take a second or two.

Controls

These tests are empty loops, just to get an idea of how long a loop takes with nothing going on inside it.

Set up beforehand: for the loops over an array, an array holding the numbers 0 to 999,999, so only the loop itself is timed.

An engine is free to notice that a loop does nothing and leave it out altogether, so a time close to zero can mean exactly that.

TestM2 MaxYour resultRun
Empty loops of a million iterations

Filling an array

Set up beforehand: nothing. Each test makes its own array, so the time includes creating it as well as filling it with the numbers 0 to 999,999.

The Map row fills a Map instead, using each index as the key. The typed arrays (there's more about them in Comparison of JavaScript binary data objects) are filled with the same numbers, each wrapping round to fit the type, so a Uint8Array holds 0 to 255 over and over. The two BigInt types can only hold BigInts, so they store BigInt(i), and the conversion is part of the time. Float16Array is only in recent browsers, and its rows say so if yours doesn't have it.

TestM2 MaxYour resultRun
Making and filling an array of a million entries

Summing an array

Set up beforehand: an array holding the numbers 0 to 999,999, made with push, and for the typed array rows, a typed array of each type filled the same way.

Timed: adding up every entry, in each of the different ways of going over an array. for … in is there because it's sometimes used on arrays by mistake: it goes over the indexes as strings.

TestM2 MaxYour resultRun
Adding up a million entries

Copying an array

Set up beforehand: an existing array of a million entries, holding the numbers 0 to 999,999.

Timed: making a second array and filling it with the same values, both by hand in a loop and with the built-in ways of copying.

JavaScript arrays

TestM2 MaxYour resultRun
Copying an array of a million numbers

Typed arrays

Each type is copied three ways: a for loop into a new array of the same type, the type's constructor given the existing array, and .set, which copies a whole array into another one in a single call.

TestM2 MaxYour resultRun
Copying a typed array of a million entries, for each type

Maps

Set up beforehand: for set, an empty Map. For everything else, a Map already holding a million entries, made again before each run because delete and clear empty it. The string keys run from "key0" to "key999999", and they're made beforehand too, in an array, so making the strings isn't part of the time.

Timed: a million calls of each method, with integer keys and with string keys, and looking up a million keys that aren't there. clear is a single call that empties all million entries at once.

TestM2 MaxYour resultRun
A million operations on a Map

Sets

Set up beforehand: for add, an empty Set, and for everything else a Set already holding a million values, made again before each run. The strings are made beforehand, as for the Map.

Timed: adding a million values, checking whether a million are there, a million that aren't, going over them all, and deleting them.

TestM2 MaxYour resultRun
A million operations on a Set

Objects as dictionaries

Before Map, a plain object was the usual way to look things up by key, and it still often is. Integer keys and string keys are kept quite differently: integer keys are stored like the entries of an array, and string keys as the object's properties.

Set up beforehand: for setting keys, an empty object. For everything else, an object already holding a million keys, made again before each run. The strings are made beforehand, as for the Map.

Timed: setting, checking with in, reading and deleting a million keys, and going over them all with for … in.

TestM2 MaxYour resultRun
A million operations on a plain object

WeakMap and WeakSet

A WeakMap or WeakSet only takes objects as keys, and doesn't stop them being garbage collected, which makes them the way to attach extra data to an object you don't own.

Set up beforehand: an array of a million small objects to use as the keys, and for has, get and delete, a collection already holding all of them, made again before each run.

Timed: a million calls of each method. The last row does the same set on an ordinary Map, for comparison.

TestM2 MaxYour resultRun
A million operations on a WeakMap and a WeakSet

Source code

All the tests are in one script, which the page loads. Each test has a setup, which isn't timed, and a run, which is; the typed array tests are made from a line of source text for each type, so each type gets its own function.

Tests

  • benchmarks.js JavaScript · 678 lines · the tests, and the code that runs them and fills in the tables

See also