Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Computation single optimization with destructuring vs application switch with single dep
(version: 1)
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"]; let cid = 0; var dep1 = { id: 0 }; var dep2 = { id: 1 }; var dep3 = { id: 2 }; function sampleArgs1(_0) { return _0; } function sampleArgs2(_0, _1) { return _0 + _1; } function sampleArgs3(_0, _1, _2) { return _0 + _1 + _2; } function sampleDes1(_0) { return _0; } function sampleDes2([_0, _1]) { return _0 + _1; } function sampleDes3([_0, _1, _2]) { return _0 + _1 + _2; } function createApplyComputation(fn, deps, isSingle) { return { id: cid++, fn, deps, apply: isSingle ? 1 : deps.length }; } function createSingleComputation(fn, deps, isSingle) { return { id: cid++, fn, deps, isSingle }; } function execApplyComputation(c) { switch (c.apply) { case 1: return c.fn(read(c.deps)); case 2: return c.fn(read(c.deps[0]), read(c.deps[1])); default: return c.fn(...c.deps.map(read)); } } 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(sampleArgs1, dep1, true));
single 1
execSingleComputation(createSingleComputation(sampleDes1, dep1, true));
apply 2
execApplyComputation(createApplyComputation(sampleArgs2, [dep1, dep2]));
single 2
execSingleComputation(createSingleComputation(sampleDes2, [dep1, dep2]));
apply 3
execApplyComputation(createApplyComputation(sampleArgs3, [dep1, dep2, dep3]));
single 3
execSingleComputation(createSingleComputation(sampleDes3, [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 dive into the provided benchmark definition and individual test cases. **Benchmark Definition** The benchmark measures the performance difference between two approaches: 1. `execApplyComputation` with `createApplyComputation` 2. `execSingleComputation` with `createSingleComputation` Both approaches are used to compute a value by calling functions that depend on some input data (`data`, `dep1`, `dep2`, and `dep3`). **Options compared** The two options being compared are: A) Using `execApplyComputation` with `createApplyComputation` B) Using `execSingleComputation` with `createSingleComputation` **Pros and Cons of each approach:** A) `execApplyComputation` with `createApplyComputation` Pros: * Can handle arbitrary numbers of dependencies * Can be more flexible when dealing with complex computations Cons: * May lead to slower performance due to the overhead of the `switch` statement * May require more memory to store the computed values for each dependency B) `execSingleComputation` with `createSingleComputation` Pros: * Typically faster than the first approach due to the reduced number of dependencies and the absence of the `switch` statement * Requires less memory to store the computed values Cons: * Only suitable for simple computations with a fixed number of dependencies * May become slower if the number of dependencies increases beyond a certain threshold **Library:** None mentioned in the benchmark definition. However, it is likely that the code uses the `read` function to access data from an array based on the dependency index. **Special JS feature or syntax:** The `createApplyComputation` and `execApplyComputation` functions use a technique called "tagged template literals" under the hood, which allows them to create functions with dynamic metadata. This is not explicitly mentioned in the benchmark definition, but it is an important concept in JavaScript. **Performance** The benchmark measures the performance difference between the two approaches by executing each test case multiple times (the exact number is not specified) and measuring the average execution rate per second for each approach. **Results:** The results show that the `execSingleComputation` with `createSingleComputation` approach is faster than the `execApplyComputation` with `createApplyComputation` approach in most cases. However, it's essential to note that this may depend on the specific use case and the size of the input data. It's also worth mentioning that the Chrome 85 browser has a relatively high execution rate per second for all test cases, which suggests that the browser is optimized for performance or that the inputs are particularly well-suited for the computations.
Related benchmarks:
withoutObjectKeys again and again
Delete vs destructure for cloned objects
Lodash omit vs es omit vs compiled omit vs rest and delete omit vs babel - a lot of different objects
Queue data structure
Compare performance of _.get to es6 destructuring
Comments
Confirm delete:
Do you really want to delete benchmark?