<script src="https://cdnjs.cloudflare.com/ajax/libs/immutable/3.8.2/immutable.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/immutability-helper@2.7.0/index.min.js"></script>
const data = new Array(300).fill(55);
let initArr = new Array(10000).fill(88);
const limitArr = (arr, limit) => (arr.length > limit ? arr.slice(-limit) : arr);
for(i=0; i<100; i++) {
initArr.push(data);
initArr = limitArr(initArr, 1000);
}
const data = new Array(300).fill(55);
let initList = Immutable.List();
initList = initList.push( (new Array(10000).fill(88)));
const limitList = (list, limit) => (list.size > limit ? list.takeLast(limit) : list);
for(i=0; i<100; i++) {
initList = limitList(initList.push(data), 1000);
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
array slice | |
immutable-js |
Test name | Executions per second |
---|---|
array slice | 7354.7 Ops/sec |
immutable-js | 97.6 Ops/sec |
Let's break down the provided JSON benchmark definition and test cases.
Benchmark Definition:
The script preparation code is empty, which means that the JavaScript interpreter will execute the provided benchmark definitions from scratch for each execution.
The HTML preparation code includes two external libraries:
Test Cases:
There are two test cases:
The benchmark definition is:
const data = new Array(300).fill(55);
let initArr = new Array(10000).fill(88);
const limitArr = (arr, limit) => (arr.length > limit ? arr.slice(-limit) : arr);
for(i=0; i<100; i++) {
initArr.push(...data);
initArr = limitArr(initArr, 1000);
}
This test case creates a large array initArr
and pushes a smaller array data
into it repeatedly. The limitArr
function is used to slice the resulting array down to a specified length.
What's being tested:
The main operation being tested here is the use of the slice()
method on an array, which creates a new array containing a subset of elements from the original array.
Options compared:
In this case, there are only two options being compared:
slice()
method to truncate the array.takeLast()
function from Immutable.js to get the last N elements of an immutable list.Pros and Cons:
Other Considerations:
The use of slice()
on large arrays may lead to performance issues due to the creation of a new array object. In contrast, Immutable.js' takeLast()
function creates an immutable list and only updates the underlying data structure when necessary, which can be more efficient in the long run.
Immutability-helper library:
In this case, the Immutability-helper library is used to simplify the usage of Immutable.js's takeLast()
function. The library provides a more concise API for working with immutable lists.
If test users special JS feature or syntax:
There are no special features or syntaxes being tested in these benchmark cases. If such features were present, they would be relevant to the discussion.
Other Alternatives:
Alternative approaches to array slicing include using Array.prototype.slice.call()
or a library like Lodash's _.slice()
. However, these approaches may not offer significant performance improvements over native slice()
.
For immutable data structures, other libraries such as Ramda or xs are also available. These libraries provide more comprehensive support for functional programming and immutability, but require additional dependencies and learning curves.