O(N)

Example:

var words = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present'];

const result = words.filter(word => isBig(word));

function isBig(word){
  	console.log("called");
	return word.length > 6;
}

Output:

"called" "called" "called" "called" "called" "called" Array ["exuberant", "destruction", "present"]

Answer from Saurabh Vaidya on Stack Overflow
🌐
Stack Overflow
stackoverflow.com › questions › 66980888 › does-javascript-have-a-filter-function-with-a-time-complexity-of-olog-n
performance - Does Javascript have a filter function with a time complexity of O(log n)? - Stack Overflow
However, since my array of strings is already sorted in alphabetical ordered, this task could theoretically be accomplished in O(log n) time. Is there any alternative Javascript ...
Discussions

JavaScript runtime complexity of Array functions - Stack Overflow
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 ... More on stackoverflow.com
🌐 stackoverflow.com
March 4, 2021
How do you find time complexity when using Javascript array methods
I believe this one is effectively quadratic. Or to be specific, O(mn), where m = arr.length and n = arr2.length. Filter is linear if its callback is constant. But in this case the callback is also linear. if the arrays increase in length the time would also increase. You can only deduce that it's not constant from this. It doesn't tell you whether it's linear, logarithmic, exponential, quadratic, etc. If arr2 is sorted, you could improve the runtime by using a binary search on it. If both arrays are sorted, I'm pretty sure you could make this effectively linear, though I'm not sure how you'd write that functionally. More on reddit.com
🌐 r/webdev
8
1
July 22, 2023
How to improve the performance of filtering an array that has 30k element?
I doubt it is the filtering itself that is particularly slow. About the only thing to optimize in your filter is to not call to Lower multiple times per item. Rather, what I think is likely the issue is that your ui doesn't handle the data well once you hit a certain number of items it tries to display. You should confirm this by testing the filtering of the items independently of the ui rendering those results. More on reddit.com
🌐 r/learnjavascript
33
11
June 15, 2023
javascript - What is the runtime complexity of this function? - Stack Overflow
Even if it helps you reduce O(2n) ... order of complexity as O(n) ... I think your interpretation of the order of operations is correct: filter, then map, then create a Set. However, in order for this algorithm to reach O(n^2), you would have to create a nested loop, for example: ... This is not the case here. In the worst case scenario (no duplicates), the algorithm will iterate the input array three times, meaning the ... More on stackoverflow.com
🌐 stackoverflow.com
January 14, 2019
🌐
Quora
quora.com › How-would-you-measure-time-complexity-for-in-built-functions-For-eg-filter-method-in-JavaScript-would-be-O-1-or-O-n
How would you measure time complexity for in-built functions? For eg. filter() method in JavaScript would be O(1) or O(n)? - Quora
Answer (1 of 3): Well, ideally the time complexity would be documented. This is the case for the C++ standard library. In the case of [code ]filter()[/code] we can reason that it must take at least N calls to the function you passed in, but the cost of that function is not under [code ]filter[/c...
🌐
JavaScript in Plain English
javascript.plainenglish.io › under-the-hood-worst-case-complexities-workings-of-popular-js-array-methods-739d5fef314a
Worst Case Complexities & Workings of Popular JS Array Methods | JavaScript in Plain English
August 9, 2022 - All elements of the array maybe changed in linear time. ... The method returns the modified array. The filter()method creates a shallow copy that includes all the elements which pass the filter function.[22] In this example, the new ‘result’ array contains element which are greater than 4.
🌐
Swift by Sundell
swiftbysundell.com › basics › time-complexity
Time Complexity | Swift by Sundell
August 9, 2019 - Since the above filter operation requires us to iterate through each element within the events array, it has a time complexity of O(n) — where n refers to the number of events that our function will be operating on.
🌐
Medium
randyperkins2k.medium.com › evaluating-time-complexity-of-native-javascript-methods-3f57f4d922f0
Evaluating Time Complexity of Native JavaScript Methods | by Randolph Perkins | Medium
December 14, 2020 - Therefore I have found it instructive to research a few commonly used methods and compile the results in one place. Hence the writing of this post! What follows are a few such methods in JavaScript, and a brief description of their characteristics as relating to their time and space complexity.
🌐
Plain English
plainenglish.io › blog › are-for-loops-better-than-arrays-filter-or-foreach-methods
Are JavaScript for loops better than filter() and forEach?()
November 15, 2020 - Just think about it: we cannot filter out items that are length 3, without actually looking at every single item. If we stop in the middle, which would be O(n/2), we have not solved the problem. Considering we have the same time complexity, we need to consider three questions: ... Performance can be calculated in JavaScript with the metric of operations per second.
Find elsewhere
Top answer
1 of 3
186

The ECMA specification does not specify a bounding complexity, however, you can derive one from the specification's algorithms.

push is O(1), however, in practice it will encounter an O(N) copy costs at engine defined boundaries as the slot array needs to be reallocated. These boundaries are typically logarithmic.

pop is O(1) with a similar caveat to push but the O(N) copy is rarely encountered as it is often folded into garbage collection (e.g. a copying collector could only copy the used part of an array).

shift is at worst O(N) however it can, in specially cases, be implemented as O(1) at the cost of slowing down indexing so your mileage may vary.

slice is O(N) where N is end - start. Not a tremendous amount of optimization opportunity here without significantly slowing down writes to both arrays.

splice is, worst case, O(N). There are array storage techniques that divide N by a constant but they significantly slow down indexing. If an engine uses such techniques you might notice unusually slow operations as it switches between storage techniques triggered by access pattern changes.

One you didn't mention, is sort. It is, in the average case, O(N log N). However, depending on the algorithm chosen by the engine, you could get O(N^2) in some cases. For example, if the engine uses QuickSort (even with an late out to InsertionSort), it has well-known N^2 cases. This could be a source of DoS for your application. If this is a concern either limit the size of the arrays you sort (maybe merging the sub-arrays) or bail-out to HeapSort.

2 of 3
18

in very simple words

push -> O(1)

pop -> O(1)

shift -> O(N)

slice -> O(N)

splice -> O(N)

Here is a complete explanation about time complexity of Arrays in JavaScript.

🌐
GeeksforGeeks
geeksforgeeks.org › javascript › es6-array-filter-method
ES6 | Array filter() Method - GeeksforGeeks
May 28, 2022 - index(Optional): The index of the current element being processed in the array. array(Optional): The array filter was called upon. thisArg(Optional): Value to use as this when executing the callback. Example 1: The filter function filters all the numeric values in the array greater than 5 · javascript · var numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; var result = numbers.filter(number => number > 5); console.log(result); Output : [ 6, 7, 8, 9, 10 ] Time complexity: O(n) Auxiliary Space: O(1) Example 2: The filter function filters all the words in the array which have length greater than 5 ·
🌐
JavaScript in Plain English
javascript.plainenglish.io › understanding-time-and-space-complexity-of-common-javascript-built-in-methods-39a3285a6409
Understanding Time and Space Complexity of Common JavaScript Built-in Methods | by Kyle Le | JavaScript in Plain English
April 14, 2023 - Here are the time and space complexities of some common JavaScript built-in methods: Array.prototype.concat(): O(n) time complexity and O(n) space complexity. This method creates a new array that includes elements from the original array(s). ...
🌐
SKOUMAL
skoumal.com › blog › performance of built-in higher-order functions map, filter, reduce, and flatmap vs. for-in loop in swift
Performance of Map, Filter, Reduce, and flatMap vs. for-in loop in Swift
December 22, 2023 - In our case, in the beginning, we have fahrenheit array and we call map function. At this moment, the time complexity is O(n), linear time, where n is the size of fahrenheit array. Then we apply the result of map function to filter function.
🌐
DEV Community
dev.to › lukocastillo › time-complexity-big-0-for-javascript-array-methods-and-examples-mlg
Time complexity Big 0 for Javascript Array methods and examples. - DEV Community
June 3, 2020 - For best-case big-omega is used (e.g., Ω(n)) and for average-case big-theta is used (e.g., Θ(n)). Notice the internal ligature of the big-theta vs that of a big O as it may be easy to mistake one for the other.
🌐
Reddit
reddit.com › r/learnjavascript › how to improve the performance of filtering an array that has 30k element?
r/learnjavascript on Reddit: How to improve the performance of filtering an array that has 30k element?
June 15, 2023 -

I'm using vuejs 2. I basically have an input box of which when I type in it, it gives me a drop down of the list of element I searched for, so say I searched for "foo" it will filter through the array of all items that has the name foo in them but if I search for "foo bar" it should give me the list of items that have the name "foo" or "bar" or "foo bar". This is business reasons.

The array of items has 30k records, this is the code I am using to do the filtering:

let keyWordsTyped = ["foo", "bar"]; // the searched keywords are placed in an array
items = items.filter((item) => { keyWordsTyped.some((keyword) => { 
            item.name.toLowerCase().includes(keyword)})})
        .map(item => {highlightItem(item)});

The thing is if I search for a single term like "foo" it works fine, the speed is fine. HOWEVER, if I search for "foo bar" it get way too slow and laggy, I guess this is because when I search for a single word the returned array is small, but if I search for two words it gets much bigger.

Is there any way to make this faster??

🌐
Medium
tharinducs.medium.com › time-complexity-of-objects-and-arrays-js-algo-02-5cd8ead23b91
Time Complexity of Objects and Arrays — JS Algo 02 | by Tharindu Senadheera | Medium
February 18, 2023 - forEach / map/filter / reduce — O(n) That’s an overview of time complexity in Arrays and Objects. See you in the next article. JavaScript · Algorithms · Arrays · Objects · Time Complexity · 33 followers · ·59 following · Senior Software ...
🌐
DEV Community
dev.to › twinfred › completing-4-javascript-filter-method-challenges-16df
Completing 4 JavaScript .filter() Method Challenges - DEV Community
June 7, 2021 - The Javascript array filter() creates a new array of elements from an existing array that meets a specified condition. Time Complexity: O(n) I decided to challenge myself with the .filter() method first because it's super useful when it comes to dealing with data.
🌐
Medium
medium.com › @chinweregina › object-array-time-and-space-complexity-in-javascript-00792792dfc5
Object/Array Time and Space Complexity in JavaScript | by Chinwe Regina | Medium
October 16, 2023 - · forEach / map/filter / reduce — O(n): These method has an O(n) and works by creating, “…a new array populated with the results of calling a provided function on every element in the calling array” (MDN).