var data = new Array(1000000);
for (var i = 0; i < data.length; i++) {
data[i] = i % 2 === 0 ? i : i + 0.5;
}
var f32 = new Float32Array(data);
var copy = f32.subarray(0, 500000);
var copy = f32.slice(0, 500000);
var copy = new Float32Array(f32.subarray(0, 500000));
var copy = new Float32Array(f32.slice(0, 500000));
var copy = new Float32Array(1000000).set(f32.subarray(0, 500000));
var copy = new Float32Array(1000000).set(f32.slice(0, 500000));
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
subarray direct | |
slice direct | |
subarray copy | |
slice copy | |
subarray set | |
slice set |
Test name | Executions per second |
---|---|
subarray direct | 42349796.0 Ops/sec |
slice direct | 6794.7 Ops/sec |
subarray copy | 6637.1 Ops/sec |
slice copy | 3359.1 Ops/sec |
subarray set | 6253.8 Ops/sec |
slice set | 3092.0 Ops/sec |
The benchmark defined in the provided JSON assesses various methods for creating and copying sections of a large array of floating-point numbers in JavaScript using the Float32Array
, part of the Typed Arrays feature of JavaScript. The benchmark focuses on the performance of different array manipulation techniques: subarray
and slice
, as well as methods that involve copying (new Float32Array()
) and setting (set()
) arrays.
Direct Subarray Creation:
var copy = f32.subarray(0, 500000);
Float32Array
without copying the data. The subarray
method returns a new TypedArray that shares the same buffer as the original, meaning both arrays reflect the same underlying data.Direct Slice Creation:
var copy = f32.slice(0, 500000);
Float32Array
. The slice
method creates a new array containing the elements from the original array, thus differing from subarray
as it does not share the buffer.Subarray with Copy:
var copy = new Float32Array(f32.subarray(0, 500000));
Float32Array
based on the results of an earlier subarray
, which means it first creates a view (like the first test case) and then copies that data into a completely new array.Slice with Copy:
var copy = new Float32Array(f32.slice(0, 500000));
Float32Array
.Subarray with Set:
var copy = new Float32Array(1000000).set(f32.subarray(0, 500000));
Float32Array
and uses the set
method to copy elements from a subarray
. The set
method allows you to copy the values of one TypedArray into another, starting at a specified index.Slice with Set:
var copy = new Float32Array(1000000).set(f32.slice(0, 500000));
Subarray:
Slice:
Copy Approaches (new Float32Array with subarray or slice):
subarray
approach, and incurs extra memory allocation overhead.Set Method:
subarray
directly due to the copying overhead.Memory Management: The direct use of subarray
can lead to increased memory efficiency, particularly in situations where large datasets are manipulated. However, developers must be cautious about unintentional side effects due to shared buffers.
Use Case Suitability: The choice between these methods may depend on the use case. For read-only access where data integrity isn't a concern, subarray
is ideal. In contrast, if independent manipulation of the data is necessary, slice
or copying methods are preferable.
Manual Copying: Implementing a manual for-loop to copy elements might provide more control over the copying process but is often less efficient than using built-in methods like slice
, set
, etc.
Typed Arrays vs Regular Arrays: For raw performance with structured numerical data, Typed Arrays (like Float32Array
) are preferred. Regular JavaScript arrays may be more convenient for general use but lack the performance optimizations of typed structures.
WebAssembly: For complex numerical computations and data manipulations beyond what JavaScript handles natively, WebAssembly could provide significant performance improvements.
This benchmark serves to inform engineers about the trade-offs present when manipulating arrays in JavaScript, helping them choose the best approach depending on their specific constraints and requirements.