Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
slice vs subarray vs new
(version: 0)
Comparing performance of:
subarray direct vs slice direct vs new array
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; var f32 = new Float32Array(data);
Tests:
subarray direct
var copy = f32.subarray(10, 20);
slice direct
var copy = f32.slice(10, 20);
new array
var copy = new Float32Array([1, 2, 3, 4, 5, 6, 7, 8, 9, 0]);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
subarray direct
slice direct
new array
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 provided benchmark definition and test cases. **Benchmark Definition:** The benchmark is testing three different approaches to create a copy of a subset of an array: 1. **Subarray (`f32.subarray(10, 20)`)**: This approach creates a new array with a specific range of elements from the original `f32` array. 2. **Slice (`f32.slice(10, 20)`)**: This approach creates a new array with a specific range of elements from the original `f32` array, similar to `subarray`. 3. **New Array (`new Float32Array([1, 2, 3, 4, 5, 6, 7, 8, 9, 0])`)**: This approach creates a new, empty `Float32Array` instance and then adds elements to it. **Options Compared:** The benchmark is comparing the performance of these three approaches: * **Pros and Cons:** + **Subarray**: Pros: efficient use of memory, as it only allocates a portion of the original array. Cons: may not be suitable for large arrays or complex data structures. + **Slice**: Similar pros and cons to `subarray`. + **New Array**: Pros: flexible, as you can create an empty array and then add elements to it. Cons: more memory-intensive, as it allocates a new array instance. **Library Used:** The benchmark uses the `Float32Array` class from JavaScript's built-in `TypedArray` API. This library provides a way to work with 32-bit floating-point numbers in an array-like data structure. **Special JS Feature/Syntax:** None mentioned in the provided benchmark definition. **Benchmark Preparation Code:** The script preparation code creates a large, random array (`data`) and then creates a `Float32Array` instance from it using `new Float32Array(data)`. **Individual Test Cases:** Each test case measures the performance of one of the three approaches: 1. **Subarray Direct**: Measures the time taken to create a copy of a specific range of elements from the original array using the `subarray()` method. 2. **Slice Direct**: Measures the time taken to create a copy of a specific range of elements from the original array using the `slice()` method. 3. **New Array**: Measures the time taken to create a new, empty `Float32Array` instance and then add elements to it. **Latest Benchmark Result:** The latest benchmark result shows the performance data for each test case: | Test Name | ExecutionsPerSecond | | --- | --- | | subarray direct | 7297383.5 | | slice direct | 7009996.0 | | new array | 3751956.5 | Note that these results are specific to Chrome 107, running on a Linux desktop platform. **Other Alternatives:** If you're looking for alternative approaches or libraries for creating copies of arrays in JavaScript, some options include: * Using `Array.prototype.slice()` with a callback function (not applicable here) * Using the `copyWithin()` method (not mentioned in this benchmark) * Using other libraries or frameworks that provide optimized array manipulation functions * Implementing custom array copying algorithms using iterative methods
Related benchmarks:
slice vs subarray vs set
slice vs subarray vs set
subarray vs slice vs copy and subarray
slice vs subarray vs ArrayBuffer copy
Comments
Confirm delete:
Do you really want to delete benchmark?