Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Option[Double]: box vs unbox
(version: 2)
Comparing performance of:
box vs unbox
Created:
one year ago
by:
Registered User
Jump to the latest result
Script Preparation code:
function None() { } None.prototype.tag = 0; const none = new None(); function Some(payload) { this.payload = payload; } Some.prototype.tag = 1; var box = []; var unbox = []; for (let i = 0; i < 1000; i++) { box.push(i % 2 == 0 ? none : new Some(i * Math.PI)); unbox.push(i % 2 == 0 ? undefined : i * Math.PI); } function square(i) { return i * i }
Tests:
box
for (let i of box) { if (i.tag === 1) { square(i.payload) } }
unbox
for (let i of unbox) { if (i !== undefined) { square(i) } }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
box
unbox
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36
Browser/OS:
Chrome 129 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
box
79414.8 Ops/sec
unbox
18041.1 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark definition and options compared. **Benchmark Definition** The benchmark tests two different approaches to working with JavaScript arrays: `box` and `unbox`. The test cases iterate over an array, perform some operations on each element, and then check if a specific condition is met. **Options Compared** * **Box vs Unbox**: The two main options compared are the "boxed" and "unboxed" approaches to handling arrays. + In the boxed approach (used in `box` test case), an array is used as a container for each element. This means that each element has its own object with properties, including a `.tag` property set to 1. When iterating over the array using `for...of`, the loop variable `i` is an instance of the `Some` class, which allows accessing the `.payload` property. + In the unboxed approach (used in `unbox` test case), an array is used as a container for some elements, but others are not wrapped in objects. When iterating over the array using `for...of`, the loop variable `i` is either an instance of the `Some` class or `undefined`. + The key difference between these approaches is how they handle undefined values in the array. In the boxed approach, all elements are wrapped in the `Some` class, ensuring that only defined elements can be accessed through the `.payload` property. In the unboxed approach, some elements may not be wrapped in objects. **Pros and Cons** * **Boxed Approach (Box)**: + Pros: Ensures that only defined elements can be accessed through the `.payload` property, preventing potential null pointer exceptions. + Cons: May incur additional overhead due to the creation of new objects for each element. * **Unboxed Approach (Unbox)**: + Pros: Can potentially reduce overhead by reusing existing objects or not creating new ones for unboxed elements. + Cons: May allow null pointer exceptions if an undefined value is iterated over. **Library and Special JS Feature** The `Some` class used in the benchmark definition is a custom library, likely created to simplify the boxed approach. Its purpose is to wrap each element in an object with a `.tag` property set to 1. This allows for easier iteration over the array using `for...of` while still maintaining control over undefined values. **Other Considerations** * **Memory Allocation**: The benchmark definition allocates memory for the `box` and `unbox` arrays, which may impact performance. * **Iteration Efficiency**: The efficiency of the iteration process can vary depending on the specific JavaScript engine and version being used. In this case, the loop variable `i` is either an instance of the `Some` class or `undefined`, which may affect how the browser handles iteration. **Alternatives** If you're looking for alternative approaches to working with arrays in JavaScript, consider: * **Using `Array.prototype.map()` and `Array.prototype.filter()`**: These methods can help simplify array processing without altering the fundamental nature of arrays. * **Using a library like Lodash**: Lodash provides a wide range of utility functions that can help simplify common tasks, such as array iteration and filtering.
Related benchmarks:
Array.prototype.push vs Array.prototype.push.apply
Array.prototype.push vs Array.prototype.push.apply1
Array.prototype.push vs Array.prototype.push.apply2
Array .push() vs .unshift() multiple
Array.prototype.push vs Array.prototype.shift
Comments
Confirm delete:
Do you really want to delete benchmark?