Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
slice v splice chunking
(version: 0)
100k list splice and shift win, they mutate list slice loose, it creates a copy of list 7.5x slower
Comparing performance of:
slice vs slice right vs splice vs splice right vs splice slice
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var input = []; var size = 3; for (var i = 0; i < 100 * 100; i++) { input.push(i); }
Tests:
slice
const fromRight = false; const result = []; let index = fromRight ? input.length % size : 0; if (index !== 0) { result.push(input.slice(0, index)); } while (index < input.length) { result.push(input.slice(index, size + index)); index += size; }
slice right
const fromRight = true; const result = []; let index = fromRight ? input.length % size : 0; if (index !== 0) { result.push(input.slice(0, index)); } while (index < input.length) { result.push(input.slice(index, size + index)); index += size; }
splice
const fromRight = false; const source = [...input]; const result = []; if (fromRight) { result.push(source.splice(0, source.length % size)); } while (input.length > 0) { result.push(input.splice(0, size)); }
splice right
const fromRight = true; const source = [...input]; const result = []; if (fromRight) { result.push(source.splice(0, source.length % size)); } while (input.length > 0) { result.push(input.splice(0, size)); }
splice slice
const fromRight = false; const source = input.slice(); const result = []; if (fromRight) { result.push(source.splice(0, source.length % size)); } while (input.length > 0) { result.push(input.splice(0, size)); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (5)
Previous results
Fork
Test case name
Result
slice
slice right
splice
splice right
splice slice
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):
Let's dive into the world of JavaScript microbenchmarks. **Benchmark Definition** The provided benchmark definition is for creating and manipulating an array of 100,000 elements. The goal is to compare the performance of different approaches to chunking and splicing the array. **Options Compared** There are four main options compared in this benchmark: 1. **splice**: This approach uses `splice` method to remove elements from the beginning of the array. 2. **slice right**: This approach uses `splice` method with a negative start index to remove elements from the end of the array, and then uses `slice` method to get a chunk of elements from the beginning of the array. 3. **splice slice**: This approach creates a copy of the entire array using `slice` method, and then uses `splice` method to remove elements from the beginning of the copied array. 4. **slice right**: This approach uses `slice` method with a negative start index to get a chunk of elements from the end of the original array. **Pros and Cons** Here's a brief summary of the pros and cons of each approach: * **splice**: + Pros: Simple and efficient, as it only requires a single call to `splice`. + Cons: Can be slow if the array is large, as it involves shifting all remaining elements. * **slice right**: + Pros: Can take advantage of optimized `splice` implementation for arrays with many elements at the beginning. + Cons: Requires more calls to `splice` and `slice`, which can be slower than a single call to `splice`. * **splice slice**: + Pros: Avoids the need for repeated calls to `splice`, but creates an unnecessary copy of the array. + Cons: Can be slower due to the creation of the copied array, even if it's only used once. **Library Usage** The benchmark uses a few libraries and features: * **Array.prototype.slice**: Used in all options to get chunks of elements from the array. * **Array.prototype.splice**: Used in all options to remove elements from the beginning of the array. * **Spread operator (...)**: Used in some options to create a copy of the original array. **Special JS Features** There are no special JS features or syntax used in this benchmark. All code is standard JavaScript, using only the built-in `Array` prototype methods and operators. **Other Alternatives** If you're interested in exploring alternative approaches, here are a few options: * **Using `map()`**: Instead of using `splice`, you could use `map()` to create a new array with chunked elements. * **Using a custom implementation**: You could implement your own optimized algorithm for chunking and splicing arrays, tailored to your specific use case. * **Using a specialized library**: There are libraries available that specialize in array manipulation and may offer optimized implementations of these operations. Keep in mind that the choice of approach will depend on your specific requirements, performance needs, and personal preference.
Related benchmarks:
slice VS splice VS shift: who is the fastest to split array
slice VS splice VS shift: who is the fastest to keep constant size (fork no string push)
slice VS splice VS shift: who is the fastest to keep constant size 100
slice VS splice VS shift: smaller list, copy last half of array
slice VS splice VS shift: who is the fastest to keep constant size [VARIANT]
Comments
Confirm delete:
Do you really want to delete benchmark?