Error Handling Benchmark
In a normal scenario, ajel
is as fast as utilizing try catch
or neverthrow
. Depending on how V8 is feeling, it can be the most performant solution!
Recursing a function which performs 1+1 millions of times shows poor performance so don't do that while using ajel
.
If performance is heavily on your mind maybe consider a different language for the given project.
Implementation
Run 1 Results:
> tsx benchmark.ts
Fib(2)
┌─────────┬──────────────┬─────────────┬────────────────────┬──────────┬─────────┐
│ (index) │ Task Name │ ops/sec │ Average Time (ns) │ Margin │ Samples │
├─────────┼──────────────┼─────────────┼────────────────────┼──────────┼─────────┤
│ 0 │ 'try_catch' │ '4,634,807' │ 215.75866815030628 │ '±1.69%' │ 463481 │
│ 1 │ 'ajel' │ '2,779,897' │ 359.72552846852784 │ '±0.99%' │ 277990 │
│ 2 │ 'neverthrow' │ '2,812,851' │ 355.5111285248286 │ '±1.08%' │ 281286 │
└─────────┴──────────────┴─────────────┴────────────────────┴──────────┴─────────┘
Fib(20)
┌─────────┬──────────────┬──────────┬───────────────────┬──────────┬─────────┐
│ (index) │ Task Name │ ops/sec │ Average Time (ns) │ Margin │ Samples │
├─────────┼──────────────┼──────────┼───────────────────┼──────────┼─────────┤
│ 0 │ 'try_catch' │ '20,761' │ 48167.08831070821 │ '±0.26%' │ 2077 │
│ 1 │ 'ajel' │ '20,759' │ 48169.70183210795 │ '±0.26%' │ 2076 │
│ 2 │ 'neverthrow' │ '20,533' │ 48700.09699431924 │ '±0.42%' │ 2054 │
└─────────┴──────────────┴──────────┴───────────────────┴──────────┴─────────┘
Allocate 500kb Files 2 times
┌─────────┬──────────────┬─────────┬───────────────────┬──────────┬─────────┐
│ (index) │ Task Name │ ops/sec │ Average Time (ns) │ Margin │ Samples │
├─────────┼──────────────┼─────────┼───────────────────┼──────────┼─────────┤
│ 0 │ 'try_catch' │ '2,310' │ 432734.4399844778 │ '±8.16%' │ 232 │
│ 1 │ 'ajel' │ '2,297' │ 435230.6347826253 │ '±8.77%' │ 230 │
│ 2 │ 'neverthrow' │ '2,417' │ 413718.4740590656 │ '±9.86%' │ 242 │
└─────────┴──────────────┴─────────┴───────────────────┴──────────┴─────────┘
------
sjel
Allocate 500kb Files 2 times
┌─────────┬─────────────┬─────────┬────────────────────┬──────────┬─────────┐
│ (index) │ Task Name │ ops/sec │ Average Time (ns) │ Margin │ Samples │
├─────────┼─────────────┼─────────┼────────────────────┼──────────┼─────────┤
│ 0 │ 'try_catch' │ '533' │ 1873371.6187653719 │ '±6.23%' │ 54 │
│ 1 │ 'sjel' │ '528' │ 1890484.647930793 │ '±8.25%' │ 53 │
└─────────┴─────────────┴─────────┴────────────────────┴──────────┴─────────┘
Run 2 Results:
> tsx benchmark.ts
Fib(2)
┌─────────┬──────────────┬─────────────┬────────────────────┬──────────┬─────────┐
│ (index) │ Task Name │ ops/sec │ Average Time (ns) │ Margin │ Samples │
├─────────┼──────────────┼─────────────┼────────────────────┼──────────┼─────────┤
│ 0 │ 'try_catch' │ '4,659,316' │ 214.62376085284828 │ '±1.71%' │ 465932 │
│ 1 │ 'ajel' │ '2,816,489' │ 355.051930451196 │ '±0.92%' │ 281649 │
│ 2 │ 'neverthrow' │ '2,822,144' │ 354.34052238047354 │ '±1.04%' │ 282215 │
└─────────┴──────────────┴─────────────┴────────────────────┴──────────┴─────────┘
Fib(20)
┌─────────┬──────────────┬──────────┬────────────────────┬──────────┬─────────┐
│ (index) │ Task Name │ ops/sec │ Average Time (ns) │ Margin │ Samples │
├─────────┼──────────────┼──────────┼────────────────────┼──────────┼─────────┤
│ 0 │ 'try_catch' │ '20,752' │ 48186.33861968972 │ '±0.21%' │ 2076 │
│ 1 │ 'ajel' │ '20,931' │ 47775.637397565726 │ '±0.19%' │ 2094 │
│ 2 │ 'neverthrow' │ '20,951' │ 47728.52949854982 │ '±0.19%' │ 2096 │
└─────────┴──────────────┴──────────┴────────────────────┴──────────┴─────────┘
Allocate 500kb Files 2 times
┌─────────┬──────────────┬─────────┬────────────────────┬──────────┬─────────┐
│ (index) │ Task Name │ ops/sec │ Average Time (ns) │ Margin │ Samples │
├─────────┼──────────────┼─────────┼────────────────────┼──────────┼─────────┤
│ 0 │ 'try_catch' │ '2,314' │ 432132.3787343913 │ '±6.11%' │ 232 │
│ 1 │ 'ajel' │ '2,408' │ 415190.9061981929 │ '±6.26%' │ 241 │
│ 2 │ 'neverthrow' │ '2,367' │ 422311.19659882557 │ '±8.50%' │ 237 │
└─────────┴──────────────┴─────────┴────────────────────┴──────────┴─────────┘
------
sjel
Allocate 500kb Files 2 times
┌─────────┬─────────────┬─────────┬───────────────────┬──────────┬─────────┐
│ (index) │ Task Name │ ops/sec │ Average Time (ns) │ Margin │ Samples │
├─────────┼─────────────┼─────────┼───────────────────┼──────────┼─────────┤
│ 0 │ 'try_catch' │ '594' │ 1682249.085108439 │ '±5.05%' │ 60 │
│ 1 │ 'sjel' │ '595' │ 1679521.369934082 │ '±3.10%' │ 60 │
└─────────┴─────────────┴─────────┴───────────────────┴──────────┴─────────┘