8

Which are the technical reasons/considerations for the sort() and reverse() JavaScript array methods to be in-place operations instead of returning a new array without modifying the original one, like the filter() or map() methods do?

I want to understand the technical reasons that drove such decision, why those methods do not follow immutability, and why was decided to modify the array in-place instead of returning a new array without modifying the original one... was it for performance reasons? technical constrains? which were the technical drivers of that decision?

6
  • 3
    What sort of answer are you looking for, and how would you use it?
    – jonrsharpe
    Commented Aug 24, 2021 at 21:54
  • 2
    @jonrsharpe I don't plan to use it, I want to understand the technical reasons that drove such decision, I want to understand why those methods do not follow immutability, and why was decided to modify the array in place instead of returning a new array without modifying the original one... was it for performance reasons? technical constrains? which are the technical drivers of that decision? I want to understand why was decided in that way and not made them immutable methods. Commented Aug 24, 2021 at 22:30
  • 1
    Then you'd have to talk to the people who made them. At a certain point it's just because they said so - there are array methods that return new arrays and those that mutate the old one, so there's clearly no fundamental technical constraint.
    – jonrsharpe
    Commented Aug 24, 2021 at 22:34
  • 2
    I just sent this Q to Brendan on Twitter. Hopefully he'll answer it :-)
    – jweyrich
    Commented Aug 24, 2021 at 22:42
  • 3
    The surprising design decision isn't so much that they're in-place (most language standard library sorts I'm familiar with are; it's generally more performant and gives the caller flexibility to avoid an allocation if they wish), it's that the in-place methods return the same object as the parameter, which can lead to confusing bugs when chained in a series of otherwise immutable calls. Most language libraries either return void on in-place algorithms or, in the case of Ruby, offer a clearly-labeled "dangerous" version that mutates and returns the argument.
    – ggorlen
    Commented Aug 25, 2021 at 18:41

2 Answers 2

5

Arrays were originally implemented as hash tables. Keys (buckets) were converted to strings and hashed. v8 still implements it as a hash table, but also supports a fixed array (ref).

The language specification (with updates) defines it as in-place, but I couldn't find a relevant design document regarding that decision.

My guess would be that the cost of duplicating the internal structure was relatively expensive at the time. It still can be depending on its size.

However, here's the official answer:

It was based on Java’s static method and also on Perl, both were in-place.

2
  • 3
    Now we'd have to dig into Java and Perl's design decision :D
    – jweyrich
    Commented Aug 24, 2021 at 22:49
  • 3
    Not really considering the extent of this question. Probably it was retained for familiarity?
    – Harshal
    Commented Aug 26, 2021 at 14:54
12

Because we can. Because it’s actually cheaper. A method that returns a different array either elements sorted or reversed takes twice as much memory.

Filtering cannot return the same array because the number of elements changes, so it returns a new array.

2
  • 2
    @MauricioRobayo actually sort does both, mutates it and returns it.
    – jonrsharpe
    Commented Aug 24, 2021 at 22:39
  • 10
    This is surely the correct answer. Let me add my two cents: if someone requires immutable sort, they can implement it easily using the mutable sort, just by copying the original array beforehand. However, the other way round it is not possible: if someone requires a more memory efficient in-place version, it would not be possible to use a less efficient immutable sort for it. So it is not very surprising that in most standard libs (not only for Javascript) the sort function is provided in an in-place fashion (same holds for reverse).
    – Doc Brown
    Commented Aug 26, 2021 at 11:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.