Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
array copy set vs block vs per item
(version: 1)
Comparing performance of:
set vs block vs per item
Created:
one year ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<!--your preparation HTML code goes here-->
Script Preparation code:
const arr1 = new Float64Array([1,2,3,4,5,6,7,8,9]); const arr2 = new Float64Array([1,2,3,4,5,6,7,8,9]);
Tests:
set
arr1.set(arr2)
block
[ arr1[0], arr1[1], arr1[2], arr1[3], arr1[4], arr1[5], arr1[6], arr1[7], arr1[8], ]=arr2
per item
arr1[0] = arr2[0]; arr1[1] = arr2[1]; arr1[2] = arr2[2]; arr1[3] = arr2[3]; arr1[4] = arr2[4]; arr1[5] = arr2[5]; arr1[6] = arr2[6]; arr1[7] = arr2[7]; arr1[8] = arr2[8];
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
set
block
per item
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36
Browser/OS:
Chrome 134 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
set
39651528.0 Ops/sec
block
25414050.0 Ops/sec
per item
94770600.0 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
The benchmark "array copy set vs block vs per item" is designed to compare the performance of three different methods for copying elements from one `Float64Array` to another in JavaScript. The test focuses on how efficiently different assignment techniques can handle this task. ### Benchmark Test Cases: 1. **Set Method (`arr1.set(arr2)`)**: - **Description**: Utilizes the built-in `set` method provided by `TypedArray` (in this case, `Float64Array`). This method copies the entire contents of one array (`arr2`) into another (`arr1`) in a single operation. - **Pros**: - Optimized for performance as it leverages internal methods potentially optimized by the JavaScript engine. - More concise and readable compared to manual copying. - **Cons**: - Less flexible for partial copying or complex conditions (e.g., copying only certain elements). 2. **Block Assignment**: - **Description**: This method utilizes a block of code to assign values from `arr2` to `arr1` using a single line for multiple assignments. - **Pros**: - Can be more explicit about the indices being copied. - In some scenarios, may provide a better performance than item-by-item assignment due to reduced function call overhead. - **Cons**: - Typically less optimized than the `set` method, especially if the JavaScript engine cannot optimize it effectively. - Still requires multiple operations, which may not be as fast as the optimized `set` method. 3. **Per Item Assignment**: - **Description**: This uses a loop-like assignment where elements are copied one at a time, explicitly assigning each item from `arr2` to `arr1`. - **Pros**: - Very clear and explicit, making it easy to understand the behavior of each assignment. - Offers the most flexibility for custom copying logic. - **Cons**: - The least performant of the three methods when dealing with large datasets due to the overhead of repeated operations and potential JavaScript engine optimizations not being utilized. ### Performance Results: Based on the latest benchmark results: - **Per Item (Execution Speed: 94,770,600 ops/sec)**: This method performed the best, which is somewhat counterintuitive given its apparent resource intensiveness, possibly due to how the JavaScript engine optimally handles such cases in the latest versions. - **Set (Execution Speed: 39,651,528 ops/sec)**: The `set` method comes in second, which is still quite efficient and demonstrates good performance. - **Block (Execution Speed: 25,414,050 ops/sec)**: The block method performed the worst, which aligns with the expectation that it is not as optimized as the others. ### Other Considerations: - Using built-in methods like `set` is generally recommended when maximum performance and readability are desired. - It's crucial to consider that performance can vary not only based on the method used but also based on the JavaScript engine and execution environment. For example, different browsers or versions may yield varying results. - Alternatives to arrays could include using libraries for immutability (like Immutable.js) or advanced data structures that may provide specific performance improvements or functionalities beyond raw speed. In summary, this benchmark illustrates the performance landscape of different array copying techniques in JavaScript, notably how the language's engines can optimize certain methods, and offers insights into their practical use within real-world applications.
Related benchmarks:
js array copy speed comparison
array assignment vs array fill
another one js array copy speed comparison
js array copy speed comparison...
js array copy speed comparison - spread
js array copy speed comparison v3
js array copy speed comparison 2
js array copy any type
Uint8Array vs Uint8ClampedArray 4 value copy
Comments
Confirm delete:
Do you really want to delete benchmark?