Skip to main content
edited body
Source Link
gsamaras
  • 73.7k
  • 50
  • 211
  • 331

In a team project we need to remove the first element of an array, thus I called Array.prototype.shift(). Now a guy saw Why is pop faster than shift?, thus suggested to first reverse the array, pop, and reverse again, with Array.prototype.pop() and Array.prototype.reservereverse().

Intiutively this will be slower (?), since my approach takes O(n) I think, while the other needs O(n), plus O(n). Of course in asymptotic notation this will be the same. However notice the verb I used, think!

Of course I could write some, use jsPerf and benchmark, but this takes time (in contrast with deciding via the time complexity signs (e.g. a O(n3) vs O(n) algorithm).

However, convincing someone when using my opinion is much harder than pointing him to the Standard (if it refered to complexity).

So how to find the Time Complexity of these methods?

For example in C++ std::reservereverse() clearly states:

Complexity

Linear in half the distance between first and last: Swaps elements.

In a team project we need to remove the first element of an array, thus I called Array.prototype.shift(). Now a guy saw Why is pop faster than shift?, thus suggested to first reverse the array, pop, and reverse again, with Array.prototype.pop() and Array.prototype.reserve().

Intiutively this will be slower (?), since my approach takes O(n) I think, while the other needs O(n), plus O(n). Of course in asymptotic notation this will be the same. However notice the verb I used, think!

Of course I could write some, use jsPerf and benchmark, but this takes time (in contrast with deciding via the time complexity signs (e.g. a O(n3) vs O(n) algorithm).

However, convincing someone when using my opinion is much harder than pointing him to the Standard (if it refered to complexity).

So how to find the Time Complexity of these methods?

For example in C++ std::reserve() clearly states:

Complexity

Linear in half the distance between first and last: Swaps elements.

In a team project we need to remove the first element of an array, thus I called Array.prototype.shift(). Now a guy saw Why is pop faster than shift?, thus suggested to first reverse the array, pop, and reverse again, with Array.prototype.pop() and Array.prototype.reverse().

Intiutively this will be slower (?), since my approach takes O(n) I think, while the other needs O(n), plus O(n). Of course in asymptotic notation this will be the same. However notice the verb I used, think!

Of course I could write some, use jsPerf and benchmark, but this takes time (in contrast with deciding via the time complexity signs (e.g. a O(n3) vs O(n) algorithm).

However, convincing someone when using my opinion is much harder than pointing him to the Standard (if it refered to complexity).

So how to find the Time Complexity of these methods?

For example in C++ std::reverse() clearly states:

Complexity

Linear in half the distance between first and last: Swaps elements.

deleted 96 characters in body
Source Link
gsamaras
  • 73.7k
  • 50
  • 211
  • 331

In a team project we need to remove the first element of an array, thus I called Array.prototype.shift(). Now a guy saw Why is pop faster than shift?, thus suggested to first reverse the array, pop, and reverse again, with Array.prototype.pop() and Array.prototype.reserve().

Intiutively this will be slower (?), since my approach takes O(n) I think, while the other needs O(n), plus O(n). Of course in asymptotic notation this will be the same. However notice the verb I used, think!

Of course I could write some, use jsPerf and benchmark, but this takes time (in contrast with deciding via the time complexity signs (e.g. a O(n3) vs O(n) algorithm).

However, convincing someone when using my opinion is much harder than pointing him to the Standard (if it refered to complexity).

So how to find the Time Complexity of these methods?

For example in C++ std::reserve() clearly states:

Complexity

Linear in half the distance between first and last: Swaps elements.

Hidden question: Why the Standard doesn't refer to the complexity of these methods?

In a team project we need to remove the first element of an array, thus I called Array.prototype.shift(). Now a guy saw Why is pop faster than shift?, thus suggested to first reverse the array, pop, and reverse again, with Array.prototype.pop() and Array.prototype.reserve().

Intiutively this will be slower (?), since my approach takes O(n) I think, while the other needs O(n), plus O(n). Of course in asymptotic notation this will be the same. However notice the verb I used, think!

Of course I could write some, use jsPerf and benchmark, but this takes time (in contrast with deciding via the time complexity signs (e.g. a O(n3) vs O(n) algorithm).

However, convincing someone when using my opinion is much harder than pointing him to the Standard (if it refered to complexity).

So how to find the Time Complexity of these methods?

For example in C++ std::reserve() clearly states:

Complexity

Linear in half the distance between first and last: Swaps elements.

Hidden question: Why the Standard doesn't refer to the complexity of these methods?

In a team project we need to remove the first element of an array, thus I called Array.prototype.shift(). Now a guy saw Why is pop faster than shift?, thus suggested to first reverse the array, pop, and reverse again, with Array.prototype.pop() and Array.prototype.reserve().

Intiutively this will be slower (?), since my approach takes O(n) I think, while the other needs O(n), plus O(n). Of course in asymptotic notation this will be the same. However notice the verb I used, think!

Of course I could write some, use jsPerf and benchmark, but this takes time (in contrast with deciding via the time complexity signs (e.g. a O(n3) vs O(n) algorithm).

However, convincing someone when using my opinion is much harder than pointing him to the Standard (if it refered to complexity).

So how to find the Time Complexity of these methods?

For example in C++ std::reserve() clearly states:

Complexity

Linear in half the distance between first and last: Swaps elements.

Corrected grammar in title
Link
Nisarg Shah
  • 14.6k
  • 6
  • 39
  • 58

Complexity of Array methods' complexitymethods

deleted 12 characters in body
Source Link
gsamaras
  • 73.7k
  • 50
  • 211
  • 331
Loading
Source Link
gsamaras
  • 73.7k
  • 50
  • 211
  • 331
Loading