Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
immutable object/set/array
(version: 0)
check performance of adding/removing items from immutable data structures like array, object, Set and Immutable.Set
Comparing performance of:
object vs array vs set vs immutable set
Created:
one year ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<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>
Script Preparation code:
function shuffle(arr) { let i = arr.length; while (i != 0) { let j = Math.floor(Math.random() * i); i--; [arr[i], arr[j]] = [ arr[j], arr[i]]; } } n = 1000 toRemove = []; for (i = 0; i < n; i++) { toRemove.push("value" + i); } shuffle(toRemove);
Tests:
object
let a = {}; // copy object and add item for (i = 0; i < n; i++) { a = {...a}; a["value" + i] = null; } // copy object and remove item toRemove.forEach(v => { a = {...a}; delete a[v]; });
array
let a = []; // copy array and add item for (i = 0; i < n; i++) { a = [...a]; a.push("value" + i); } // copy array and remove item toRemove.forEach(v => { a = a.filter(x => x !== v); });
set
let a = new Set(); // copy set and add item for (i = 0; i < n; i++) { a = new Set(a); a.add("value" + i); } // copy set and remove item toRemove.forEach(v => { a = new Set(a); a.delete(v); });
immutable set
let a = Immutable.Set(); // add item for (i = 0; i < n; i++) { a = a.add("value" + i); } // remove item toRemove.forEach((v) => { a = a.delete(v); });
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
object
array
set
immutable set
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Mobile Safari/537.36
Browser/OS:
Chrome Mobile 128 on Android
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
object
1.2 Ops/sec
array
43.9 Ops/sec
set
40.7 Ops/sec
immutable set
414.5 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Overview** The provided JSON represents a benchmarking test for JavaScript microbenchmarks, specifically focusing on the performance of adding and removing items from immutable data structures such as arrays, objects, sets, and an immutable set. The test is designed to measure the execution time and frequency of these operations. **Benchmark Definition** The benchmark definition outlines three main types of tests: 1. **Immutable Object/Array**: Measures the performance of adding or removing items from an object or array using the `Object.assign()` method or spread operator (`{...obj}`), respectively. 2. **Mutable Array**: Measures the performance of adding or removing items from a regular array using the `push()` and `filter()` methods, respectively. 3. **Mutable Set**: Measures the performance of adding or removing items from a regular set using the `add()` and `delete()` methods, respectively. 4. **Immutable Set**: Measures the performance of adding or removing items from an immutable set (using Immutable.js) using the `add()` and `delete()` methods, respectively. **Options Compared** The test compares different approaches for each data structure: * Mutable arrays: `push()` vs. `filter()` * Mutable sets: `add()` vs. `delete()` * Immutable objects: `Object.assign()` vs. spread operator (`{...obj}`) * Immutable sets: `add()` vs. `delete()` (using Immutable.js) **Pros and Cons** Here's a brief summary of the pros and cons for each approach: 1. **Immutable Object/Array**: Pros - thread-safe, predictable behavior; Cons - slower than mutable versions due to object creation. 2. **Mutable Array**: Pros - fast, efficient; Cons - not thread-safe, may lead to unexpected behavior. 3. **Mutable Set**: Pros - fast, efficient; Cons - not thread-safe, may lead to unexpected behavior. 4. **Immutable Set**: Pros - thread-safe, predictable behavior; Cons - slower than mutable versions due to Immutable.js overhead. **Library and Its Purpose** The `immutable` library is a popular JavaScript implementation of immutable data structures. It provides a set of data structures that ensure data consistency and predictability by ensuring that once created, the data structure cannot be modified. The library is useful for applications requiring predictable behavior in concurrent environments. The `immutability-helper` library is a utility library that extends Immutable.js, providing additional helper methods for working with immutable data structures. It simplifies operations like adding or removing items from sets and provides other convenience functions. **Special JS Feature/Syntax** None of the test cases use special JavaScript features or syntax beyond standard ECMAScript syntax. **Other Alternatives** If you're looking for alternative JavaScript libraries or approaches, consider: 1. **Lodash**: A utility library that includes functions for working with arrays and sets. 2. **Underscore.js**: Another utility library that provides functions for working with arrays and sets. 3. **Mocha**: A testing framework that allows you to write test cases for your benchmarking tests. 4. **Benchmark.js**: A popular JavaScript benchmarking library that allows you to create and run benchmarks. Keep in mind that these alternatives may have different performance characteristics or trade-offs, so be sure to evaluate them based on your specific use case.
Related benchmarks:
object spread vs immutable-js set (large)
array slice vs immutable-js takeLast
Immutable.Set Union vs Constructing a new plain JS Set
Immutable.Set Union vs Constructing a new plain JS Set - small
object spread vs immutable-js set vs native Map copy
Comments
Confirm delete:
Do you really want to delete benchmark?