What is the most concise and efficient way to find out if a JavaScript array contains an object? This is the only way I know to do it: function contains(a, obj) { for (var i = 0; i < a.length; i++) { if (a[i] === obj) {...

What is the most concise and efficient way to find out if a JavaScript array contains a value? This is the only way I know to do it: function contains(a, obj) { for (var i = 0; i < a.length; i++) { if (a[i] === obj) { r...

Is the runtime complexity defined by the JS standard on common Array functions like push, pop, shift, slice or splice? Esp. I'm interested in removing and inserting entries at random positions. If the complexity is not defined, what can I expec...

If I remove one element from an array using splice() like so: arr.splice(i, 1); Will this be O(n) in the worst case because it shifts all the elements after i? Or is it constant time, with some linked list magic underneath?...

In some of the projects I'm working on as part of my day job, I need to access data in very large JS objects (on the order of thousands of key-value pairs). I'm trying to improve the efficiency of my code, so I came up with a few questions:...

I have the following JavaScript on my site so that when certain specific searches are performed, the answer is hardcoded to a specific page: function redirect() { var input = document.getElementById('searchBox').value.toLowerCase();...

I wrote two prime finder functions and the sieve only performs about 10% better. I'm using two optimizations for the simple version. Don't check even numbers Only check up to the square root or j * j <= i. ( equivalent ) and one opti...

I have this function that returns the minimum difference between the sum of two parts of an array based on the position P the Array is partitioned. The programmed is tested to to run at O(N * N) time complexity and 0% performance though O(N) is was e...

Assuming there are no crazy optimizations (I'm looking at you Chrome). I'm talking about raw, nasty, ain't-broke-don't-fix-it, ie v6 javascript, cost. The lower limit being: document.getElementById() Versus: document.getEleme...

My function should return the missing element in a given array range. So i first sorted the array and checked if the difference between i and i+1 is not equal to 1, i'm returning the missing element. // Given an array A such that: // A[0] = 2 //...

©2020 All rights reserved.