Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Read/Write cycle for fixed size arrays
(version: 0)
10 writes per 1 read
Comparing performance of:
slice immutable vs splice immutable vs shift-while immutable vs slice mutable vs splice mutable vs shift-while mutable vs circular buffer
Created:
5 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var count = 120000; var list = new Array(count).fill(100);
Tests:
slice immutable
var insertCount = 0; for (var i = 0; i < 10; ++i) { if (list.length > count - 1) { list=list.slice(0, count - 1) list.push(insertCount++) } else { list = [...list, insertCount++]; } } var foo = list;
splice immutable
var insertCount = 0; for (var i = 0; i < 10; ++i) { list = [...list, insertCount++] if (list.length > count) { list.splice(0, list.length - count); } } var foo = list;
shift-while immutable
var insertCount = 0; for (var i = 0; i < 10; ++i) { list = [...list, insertCount++] while (list.length > count) { list.shift(); } } var foo = list;
slice mutable
var insertCount = 0; for (var i = 0; i < 10; ++i) { list=list.slice(0, count) list.push(insertCount++) } var foo = list;
splice mutable
var insertCount = 0; for (var i = 0; i < 10; ++i) { list.push(insertCount++) if (list.length > count) { list.splice(0, list.length - count); } } var foo = list;
shift-while mutable
var insertCount = 0; for (var i = 0; i < 10; ++i) { list.push(insertCount++) while (list.length > count) { list.shift(); } } var foo = list;
circular buffer
var idx = 0; var insertCount = 0; for (var i = 0; i < 50; ++i) { list[idx] = insertCount++; idx = (idx + 1) % count; } var foo = [...list.slice(idx), ...list.slice(0, idx)];
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (7)
Previous results
Fork
Test case name
Result
slice immutable
splice immutable
shift-while immutable
slice mutable
splice mutable
shift-while mutable
circular buffer
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):
**Benchmark Overview** The provided benchmark, "Read/Write cycle for fixed size arrays", is designed to test the performance of different JavaScript array operations. The benchmark consists of six individual test cases, each measuring the execution time of a specific operation: 1. `slice immutable` 2. `splice immutable` 3. `shift-while immutable` 4. `slice mutable` 5. `splice mutable` 6. `shift-while mutable` **Options Compared** The benchmark compares the performance of different approaches for each operation: * Immutable operations (using `slice` or spread operators) vs. Mutable operations (using indexing and `push` methods) Each test case measures the execution time of a single operation, allowing users to compare the performance of each approach. **Pros and Cons of Different Approaches** Here's a brief overview of the pros and cons of each approach: 1. **Immutable Operations** * Pros: + Create a new array object, avoiding mutability + Can be more efficient for large arrays due to less overhead * Cons: + May lead to increased memory allocation and garbage collection 2. **Mutable Operations** * Pros: + Can be faster for small to medium-sized arrays + May reduce memory allocation overhead * Cons: + Mutability can lead to unexpected behavior or errors **Library Usage** The `slice` method is used in test cases 1 and 4, which are both immutable operations. The `splice` method is used in test cases 2 and 5, which are both mutable operations. **Implementation Details** The benchmark implementation likely uses a simple array creation loop to generate the data for each test case. The actual JavaScript engine may optimize certain operations or use caching to improve performance. **Performance Results** The provided execution times show that: * Immutable `slice` operations (test cases 1 and 4) are generally faster than mutable `splice` operations (test cases 2 and 5) * Shift-based immutable operations (test case 3) are slower than slice-based immutable operations * The order of the results for similar approaches (e.g., slice vs. splice) is consistent across different browsers and versions. Keep in mind that these results may vary depending on the specific JavaScript engine, browser version, and hardware used to run the benchmark.
Related benchmarks:
array test
Performance of JavaScript .forEach, for in
Performance of JavaScript .forEach, for in v3
Array fill map, vs for i loop
Array fill map, vs while loop
Comments
Confirm delete:
Do you really want to delete benchmark?