Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Computation single optimization vs application fn
(version: 0)
Comparing performance of:
apply 1 vs single 1 vs apply 2 vs single 2 vs apply 3 vs single 3
Created:
5 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
const data = ["hello", " ", "world"]; const applicators = [ ({ fn }) => fn(), ({ fn, deps }) => fn(read(deps[0])), ({ fn, deps }) => fn(read(deps[0]), read(deps[1])), ({ fn, deps }) => fn(...deps.map(read)) ] let cid = 0; var dep1 = { id: 0 }; var dep2 = { id: 1 }; var dep3 = { id: 2 }; function sample1(_0) { return _0; } function sample2(_0, _1) { return _0 + _1; } function sample3(_0, _1, _2) { return _0 + _1 + _2; } function createApplyComputation(fn, deps) { return { id: cid++, fn, deps, apply: applicators[deps.length] }; } function createSingleComputation(fn, deps, isSingle) { return { id: cid++, fn, deps, isSingle }; } function execApplyComputation(c) { return c.apply(c); } function execSingleComputation(c) { if (c.isSingle) { return c.fn(read(c.deps)); } return c.fn(...c.deps.map(read)); } function read(s) { return data[s.id]; }
Tests:
apply 1
execApplyComputation(createApplyComputation(sample1, [dep1]));
single 1
execSingleComputation(createSingleComputation(sample1, dep1, true));
apply 2
execApplyComputation(createApplyComputation(sample2, [dep1, dep2]));
single 2
execSingleComputation(createSingleComputation(sample2, [dep1, dep2]));
apply 3
execApplyComputation(createApplyComputation(sample3, [dep1, dep2, dep3]));
single 3
execSingleComputation(createSingleComputation(sample3, [dep1, dep2, dep3]));
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (6)
Previous results
Fork
Test case name
Result
apply 1
single 1
apply 2
single 2
apply 3
single 3
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark definition and test cases to understand what is being tested. **Benchmark Definition** The benchmark definition provides a script that defines several functions (`sample1`, `sample2`, and `sample3`) and two helper functions (`createApplyComputation` and `createSingleComputation`). These functions are then used to create and execute two types of computations: single and apply computations. * **Single Computation**: A single computation is an individual function execution, where the input values are directly passed to the function. In this case, it's `c.fn(read(c.deps))` if `c.isSingle` is false. * **Apply Computation**: An apply computation is a nested function call, where multiple arguments are passed to a single function. It's `c.apply(c)`. The script also defines some data (`data`) and two dependency objects (`dep1`, `dep2`, and `dep3`). **Test Cases** There are six test cases: 1. **Apply 1**: Tests the apply computation with `sample1` and a single dependency. 2. **Single 1**: Tests the single computation with `sample1` and only one dependency. 3. **Apply 2**: Tests the apply computation with `sample2` and two dependencies. 4. **Single 2**: Tests the single computation with `sample2` and two dependencies. 5. **Apply 3**: Tests the apply computation with `sample3` and three dependencies. 6. **Single 3**: Tests the single computation with `sample3` and three dependencies. **Libraries and Special Features** The script uses a custom `read` function to retrieve data from an array based on an ID. This suggests that the script is designed to work with arrays of data, where each element has an ID corresponding to its position in the array. There are no special JavaScript features or syntax mentioned in the script. **Options Compared** The benchmark compares two approaches: 1. **Apply Computation**: Passes multiple arguments to a single function. 2. **Single Computation**: Directly passes input values to individual functions. The pros and cons of these approaches depend on the specific use case, but some general observations can be made: * Apply computation: + Pros: Can be more efficient for certain types of computations where arguments are already prepared. + Cons: May lead to increased function call overhead due to the additional argument passing. * Single computation: + Pros: Provides direct access to input values and avoids unnecessary argument passing. + Cons: Requires multiple function calls, which may incur higher overhead. **Observations from Benchmark Results** The benchmark results show that the apply computation is generally faster than the single computation for all test cases. However, there are some variations in performance between the different tests. It's possible that this is due to differences in implementation or optimization strategies. Overall, the benchmark suggests that the apply computation approach may be more efficient in many cases, but it's essential to consider the specific requirements and constraints of the use case before making a decision.
Related benchmarks:
single-upstream-vs-switch-apply-2
Computation single optimization with destructuring vs application fn
Computation single optimization with destructuring vs application switch with single dep
Computation single optimization with destructuring vs application fn - 1000 runs
Comments
Confirm delete:
Do you really want to delete benchmark?