Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array Stack/Queue VS Custom Stack/Queue
(version: 3)
Compares Arrays & Shift/Pop against custom non-mutative Stack/Queue implementations.
Comparing performance of:
Array Shift vs Array Pop vs Custom Shift vs Custom Pop
Created:
8 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
function ProxyHandler(array, take, add) { const self = this; this.take = () => take.call(this); this.add = value => add.call(this, value); this.array = array; this.length = array.length; this.index0 = 0; } ProxyHandler.prototype.get = function (array, accessor, proxy) { switch (accessor) { case "take": return this.take; break; case "add": return this.add; break; case "length": return this.length; default: return array[this.index0 + Number(accessor)]; } }; ProxyHandler.prototype.set = function (array, index, value, proxy) { const newIndex = this.index0 + Number(index); if (newIndex >= this.length) { this.length = newIndex + 1; } array[newIndex] = value; return true; }; function takeQueue() { if (this.length > 0) { this.length--; this.index0++; return this.array[this.index0 - 1]; } return undefined; } function takeStack() { if (this.length > 0) { this.length--; return this.array[this.index0 + this.length]; } return undefined; } function add(value) { this.array[this.index0 + this.length] = value; this.length++; } function createStack(iterable = null) { if (iterable !== null) { var array = Array.from(iterable); } else { var array = []; } return new Proxy(array, new ProxyHandler(array, takeStack, add)); } function createQueue(iterable = null) { if (iterable !== null) { var array = Array.from(iterable); } else { var array = []; } return new Proxy(array, new ProxyHandler(array, takeQueue, add)); } var a1 = new Array(50000000); var a2 = new Array(50000000); var q = createQueue(new Array(50000000)); var s = createStack(new Array(50000000));
Tests:
Array Shift
a1.shift(); a1.shift(); a1.shift();
Array Pop
a2.pop(); a2.pop(); a2.pop();
Custom Shift
q.take(); q.take(); q.take();
Custom Pop
s.take(); s.take(); s.take();
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
Array Shift
Array Pop
Custom Shift
Custom Pop
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 benchmark is designed to compare two approaches: using built-in Array methods (Shift, Pop) versus custom non-mutative Stack/Queue implementations. **Options Compared** 1. **Built-in Array methods**: `shift()`, `pop()` 2. **Custom non-mutative Stack/Queue implementations**: Custom `take()` and `add()` functions for both stacks and queues. **Pros and Cons of Each Approach** **Built-in Array methods** Pros: * Fast and efficient * Well-tested and optimized by the JavaScript engine * Easy to implement Cons: * May not be suitable for all use cases, especially those requiring non-mutability or specific performance characteristics. * May incur overhead due to dynamic method calls. **Custom non-mutative Stack/Queue implementations** Pros: * Can provide better performance characteristics, such as O(1) time complexity for `take()` and `add()`. * Non-mutative, which can be beneficial in certain scenarios. * Allows for more control over the implementation. Cons: * Requires manual memory management and handling of edge cases. * May require additional setup and configuration. **Library Used** The custom non-mutative Stack/Queue implementations use the `ProxyHandler` API to create a proxy object that intercepts and modifies method calls. The `take()` and `add()` functions are implemented using this proxy handler, which allows for efficient and non-mutative operations on the underlying array. **Special JS Feature** The benchmark uses custom JavaScript features, such as `ProxyHandler`, to implement the non-mutative Stack/Queue implementations. This requires a good understanding of the feature and its use cases. **Other Alternatives** If you're interested in exploring alternative approaches, here are a few options: 1. **Using native WebAssembly (WASM) for performance-critical code**: WASM provides a platform-agnostic way to execute compiled code, which can be useful for performance-critical benchmarks. 2. **Implementing custom data structures using C++ or other languages**: If you need extreme performance characteristics, implementing custom data structures in a language like C++ can provide better results. Keep in mind that each approach has its own trade-offs and considerations. It's essential to choose the best approach based on your specific use case and requirements.
Related benchmarks:
Array.prototype.slice vs spread operator proper
Array.slice vs Array.concat vs Spread operator
Array.slice vs Array.concat vs Spread operator v2
Spread Operator VS Array.prototype.slice() VS Array.prototype.slice(0)
Spread Operator VS Array.prototype.slice() VS Array.prototype.map()
Comments
Confirm delete:
Do you really want to delete benchmark?