【Skill Boost Plan】SB03 javascript declarative cost
Understand the performance of writing a declarative style coding in js.
Completing this exercise could help you to figure out the following questions:
-
Do you know the trade-off of writing a functional implementation in js?
-
Do you know how to write a fastest and super efficient implementation in js?
-
Do you know the js libraries you used every day added how much overhead?
-
Is optimizing a functional implementation worth trying in js?
Given that, there are two black-box functions and one array with the following declarations,
const arr = [
{
id: number,
name: string,
someNum: number,
},
]
const someTransform = ({ someNum }) => number | null
const someCalculation = number => number
The possible arr
sizes are in {1, 10, 100, 1000, 10000, 100000, 1000000}
.
- apply a black-box function
someTransform
on each value inarr
- remove null values in
arr
- remove duplicate values in
arr
- apply a black-box function
someCalculation
on each value inarr
- calculate the sum of the values
- return the total sum
For a pure learning purpose, you are suggested to develop three versions:
-
A "fastest" version to compute all the tasks.
-
A "declarative" style version that is the simplest to understand, debug and trace. (functional approach)
-
Based on 2, try to optimize 2 to a faster "declarative" style implementation.
P.S. Optimization often requires a trade-off but try to sacrifice as little as possible to optimize the performance and maintain the simplicity. You could use any js library to complete those implementations.
-
If you used vanilla javascript for those implementations, try to use famous libraries like
lodash
andramda
to implement and benchmark in ALL array sizes again. -
If you used
lodash
orramda
for those implementations, try to use vanilla javascript to implement and benchmark in ALL array sizes again. -
If you think a lazy evaluation is an important feature in general. Try to use vanilla javascript to implement with iterator pattern.
- Clone this project and install npm modules
git clone https://github.com/gaplotech/SB03-js-declarative-cost
yarn install
-
Complete your implementation in
src/perf-impl.js
-
Test your implementation correctness
yarn test
- Test the performance
# roughly, shorter iteration and small samples
yarn benchmark:rough
# precisely, minimum 200 samples
yarn benchmark:precise
- You could submit your pull request and ask for review and discuss before the official answer release.
Check Answers
A short answer and conclusion will be posted on 7/Sep 2020.
(TLDR;) If you have no time to complete this exercise, you could read my comprehensive R&D findings(implemented 20 versions) via Patreon.
To get an accurate benchmark, we need to make sure the benchmark environment has no significant workload, no performance thermal throttling, and no affection with noisy neighbour(if VM). That's why I have rented a packet bare metal machine (t1.small.x86) in NTR to perform the benchmark with a minimum 1000 samples for each benchmark.
As a result, the whole benchmark 1000 sample * 20 implementations * 7 sizes
takes 16.44
hours to run! The final result is stunning and fascinating! Combined with the source code
provided in Patreon, it is a good insight into declarative style, performance, and
functional optimizations techniques. It helps you to make a better development decision to
solve different challenges.
Early Access - JS Benchmark in 20 different implementations for same purpose