<script type='text/javascript' src='https://cdnjs.cloudflare.com/ajax/libs/immutable/3.8.2/immutable.min.js'></script>
var testObj = {
item1: '1234',
item2: '1234',
item3: '1234',
item4: '1234',
item5: '1234',
item6: '1234',
item7: '1234',
item8: '1234',
item9: '1234',
item10: '1234',
item11: '1234',
item12: '1234',
item13: '1234',
item14: '1234',
item15: '1234',
item16: '1234',
item17: '1234',
item18: '1234',
item19: '1234',
item20: '1234',
item21: '1234',
item22: '1234',
item23: '1234',
};
var testImmutable = Immutable.fromJS(testObj);
var result = testImmutable.toJS();
var testObj = {
item1: testImmutable.get('item1'),
};
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Immutable toJS | |
Immutable get |
Test name | Executions per second |
---|---|
Immutable toJS | 246044.2 Ops/sec |
Immutable get | 11454698.0 Ops/sec |
Let's break down the provided benchmarking test cases and explain what is being tested.
Overview
The test case compares two approaches for interacting with an Immutable.js object: using the toJS()
method to convert it to a plain JavaScript object, and using the get()
method to access specific properties directly on the Immutable object.
Immutable.js Library
The Immutable.js library is a functional programming library for JavaScript that provides data structures such as Maps, Sets, lists (sequences), tuples (pairs), and more. Its primary goal is to provide a safer alternative to traditional mutable objects like arrays or objects, by ensuring that data is immutable and predictable.
toJS()
Method
The toJS()
method converts an Immutable object to a plain JavaScript object. This approach is useful when you need to perform operations on the resulting object as if it were a native JavaScript object. However:
get()
Method
The get()
method returns the value of a specific key or index within an Immutable object. This approach is useful when you need to access specific parts of the data without having to iterate over the entire collection. However:
toJS()
, since it directly accesses the underlying data structure.Other Considerations
When choosing between these approaches, consider the following:
get()
might be a better choice. However, if you need to perform complex operations on the resulting data, toJS()
might be more suitable.get()
ensures that your code is safer and more predictable, since it avoids mutable state. However, this may come at a cost in terms of performance.Example Code
Here's an example of how you could modify the original test case to include additional benchmarking:
// Define two functions for testing
function getTest() {
const result = testImmutable.get('item1');
return result;
}
function toJSTest() {
var result = testImmutable.toJS();
// Perform operations on result as if it were a native JavaScript object
console.log(result.item1);
}
// Create an Immutable object
var testImmutable = Immutable.fromJS(testObj);
// Define the benchmarking test cases
var benchmarks = [
{
"Benchmark Definition": "getTest();",
"Test Name": "Immutable get"
},
{
"Benchmark Definition": "toJSTest();",
"Test Name": "Immutable toJS"
}
];
// Run the benchmarking tests
benchmarks.forEach(function(benchmark) {
// Comment out or uncomment as needed based on desired test output.
// console.log(`Running ${benchmark.TestName}...`);
});
Alternatives
Other alternatives for interacting with Immutable.js objects include:
map()
: Similar to get()
, but applies a function to each element in the collection.filter()
: Applies a predicate function to each element in the collection, returning a new collection containing only elements that pass the test.reduce()
: Accumulates values from the collection using a callback function.These methods provide additional ways to interact with Immutable objects and can be useful depending on your specific use case.