Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Closures vs Calls (with setup incl)
(version: 0)
Comparing performance of:
Call the same calc function with different arguments vs Call separate closures for each calc
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
window.data = [] for (let i = 0; i < 1000; i++) { window.data.push({a: Math.floor(Math.random() * 100), b: Math.floor(Math.random() * 100)}) }
Tests:
Call the same calc function with different arguments
const { data } = window class DataItem { a b constructor(a, b, c) { this.a = a this.b = b } calc(c) { return `${this.a} + ${this.b} + ${c} = ${this.a + this.b + c}` } } const fnCalls = [] for (let c = 0; c < data.length; c++) { const { a, b } = data[c] const dataItem = new DataItem(a, b) fnCalls.push([dataItem.calc, dataItem, [c]]) } fnCalls.forEach(([fn, thisArg, args] = call) => console.log(fn.apply(thisArg, args)) )
Call separate closures for each calc
const { data } = window const fns = [] for (let c = 0; c < data.length; c++) { const {a, b} = data[c]; const fn = function() { return `${a} + ${b} + ${c} = ${a + b + c}` } fns.push(fn) } fns.forEach(fn => console.log(fn()))
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Call the same calc function with different arguments
Call separate closures for each calc
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 explaining what is tested on the provided JSON, comparing options, and discussing pros and cons. **Benchmark Definition Overview** The benchmark measures the performance difference between two approaches: calling a closure (function) with different arguments versus calling separate closures for each calculation. The test creates an array of 1000 random data points (`window.data`) and uses them to set up two scenarios: 1. **Call the same calc function with different arguments**: This approach reuses the same closure function (`calc`) with different input values, passing `this` as an argument. 2. **Call separate closures for each calc**: This approach creates a new closure function for each data point, using `this` as an argument. **Options Compared** The two options being compared are: A) Reusing the same closure function (`calc`) with different arguments B) Creating separate closures for each calculation **Pros and Cons of Each Approach** 1. **Reusing the same closure function (A)**: * Pros: + Reduces memory allocation and garbage collection overhead + Can lead to better caching and inlining of the closure function * Cons: + May introduce slower startup times due to the initial function creation + Can make debugging more complex if the closure function is not properly initialized 2. **Creating separate closures for each calculation (B)**: * Pros: + Easier to debug and understand, as each closure has a clear scope + Allows for more flexibility in handling different input values * Cons: + Increases memory allocation and garbage collection overhead + May lead to slower performance due to the creation of multiple closures **Library Used** There is no explicit library mentioned in the benchmark definition. However, it appears that the `window.data` array is being used to store random data points. **Special JS Feature or Syntax** None are explicitly mentioned. **Other Considerations** * **Warm-up overhead**: Both approaches may have some warm-up overhead due to the initial function creation or initialization. * **Cache optimization**: The browser's cache may optimize the performance of both approaches, making it harder to distinguish between them. **Alternatives** Some possible alternatives to consider: * Using a different data structure, such as an object with method properties, instead of an array with closure functions * Adding more complex logic or computations to the closure functions to make the benchmark more realistic * Using a different browser or version to see how it affects the performance Keep in mind that these alternatives may not provide meaningful insights into the performance characteristics of the original benchmark.
Related benchmarks:
A.push(B) V.S. [ ...A, B ]
In loop, push(...) vs spread
In loop, push(...) vs spread v2
Adding to an existing array from another (loop, for of, forEach, spread) [1]
push() vs [...] for large arrays
Comments
Confirm delete:
Do you really want to delete benchmark?