A collection of little experiments and tools that may help a TypeScript developer throughout the weekend afternoons.
If you want to compare the performance of multiple approaches to a problem:
- Open
src/benchmark.ts - Update the
add testssection to contain the experiments that you wish to measure- Be careful to ensure that individual tests are identical in all ways except for the variation that is being tested.
- Run
yarn benchmarkoryarn benchmark:dev
Typical results look something like this:
Starting benchmarks for INPUT_SIZE 10000
⏱️ Measurements
┌───────────────────────┬───────────┬─────────────────┬──────────────┐
│ (index) │ hz │ margin of error │ runs sampled │
├───────────────────────┼───────────┼─────────────────┼──────────────┤
│ map.has │ '8.59e+8' │ '±1.59%' │ 88 │
│ array.includes │ '8.03e+8' │ '±0.99%' │ 79 │
│ set.has │ '7.18e+7' │ '±0.96%' │ 95 │
│ object.hasOwnProperty │ '2.13e+7' │ '±1.27%' │ 92 │
└───────────────────────┴───────────┴─────────────────┴──────────────┘
🏁 Comparison Matrix
┌───────────────────────┬─────────┬─────────┬─────────┬─────────┐
│ (index) │ map.has │ arryncl │ set.has │ objctOP │
├───────────────────────┼─────────┼─────────┼─────────┼─────────┤
│ map.has │ 1 │ 1.07 │ 11.96 │ 40.33 │
│ array.includes │ 0.94 │ 1 │ 11.19 │ 37.71 │
│ set.has │ 0.08 │ 0.09 │ 1 │ 3.37 │
│ object.hasOwnProperty │ 0.02 │ 0.03 │ 0.3 │ 1 │
└───────────────────────┴─────────┴─────────┴─────────┴─────────┘☝️ This can be interpreted like so...
For an INPUT_SIZE of 10000:
- The code inside the
map.hastest can be executed at a rate of ~8.59e+8times per second. - ...
- The code inside the
object.hasOwnPropertytest can be executed at a rate of ~2.13e+7times per second. - ...
- The code inside the
map.hastest is ~40.33times faster than the code inside theobject.hasOwnPropertytest.
Given those results, you can choose which alternative approach to use. 🚀
If you want to try something out:
- Open
src/scratchpad.ts - Update the code with the thing that you want to try out
- Run
yarn scratchpadoryarn scratchpad:dev
Some experiments have their own [whateverName].md docs.