What’s the time complexity of array.splice() in Google Chrome?

Worst case should be O(n) (copying all n-1 elements to new array). A linked list would be O(1) for a single deletion. For those interested I’ve made this lazily-crafted benchmark. (Please don’t run on Windows XP/Vista). As you can see from this though, it looks fairly constant (i.e. O(1)), so who knows what they’re doing … Read more

Time complexity of a recursive algorithm

Analyzing recursive functions (or even evaluating them) is a nontrivial task. A (in my opinion) good introduction can be found in Don Knuths Concrete Mathematics. However, let’s analyse these examples now: We define a function that gives us the time needed by a function. Let’s say that t(n) denotes the time needed by pow(x,n), i.e. … Read more

If strings are immutable in .NET, then why does Substring take O(n) time?

UPDATE: I liked this question so much, I just blogged it. See Strings, immutability and persistence The short answer is: O(n) is O(1) if n does not grow large. Most people extract tiny substrings from tiny strings, so how the complexity grows asymptotically is completely irrelevant. The long answer is: An immutable data structure built … Read more

Are there any cases where you would prefer a higher big-O time complexity algorithm over the lower one?

There can be many reasons to prefer an algorithm with higher big O time complexity over the lower one: most of the time, lower big-O complexity is harder to achieve and requires skilled implementation, a lot of knowledge and a lot of testing. big-O hides the details about a constant: algorithm that performs in 10^5 … Read more

Is there anything that guarantees constant time for accessing a property of an object in JavaScript?

Is there anything in the JavaScript guaranteeing that the values are looked up in O(1) time? No. JavaScript does not give any complexity guarantees whatsoever, except for ES6 collections. I know the access operator [] gives a seasoned programmer the impression that he’s dealing with an O(1) lookup structure Yes you are, this is a … Read more