GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||
GeezerVison is showing github.com — feat: add toSorted() explanation and examples. by AlexNT-maker · Pull Request #3963 · javascript-tutorial/en.javascript.info Conversation
Sorry, something went wrong. Uh oh!
There was an error while loading. Please reload this page. All reactions- Briefly explains the concept of immutability and why mutating original data is avoided in modern JS. - Introduce` toSorted()` as the modern alternative to `sort()` . - Includes a runnable code block (`js run`) demonstrating how the original array remains untouched compared to the new sorted array.
|
There was an error while loading. Please reload this page.
|
|
||
| ### toSorted | ||
|
|
||
| In modern JavaScript, we generally avoid mutating original data, as it can lead to unexpected side effects and bugs. Because the traditional `sort()` method modifies the array in place, a new modern method was introduced. The `toSorted()` method elegantly solves this problem by returning a completely new, sorted array, leaving the initial data safely intact. It is also more optimized than the older workaround of manually cloning the array before sorting. |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
The claim that toSorted() is “more optimized” than copying and sorting isn't guaranteed. MDN describes it as the copying version of sort(), whose complexity depends on the implementation; readers may mistake this for a performance guarantee over [...arr].sort(). Could we remove the performance claim and keep the clear non-mutating behavior explanation?
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Done! Thanks for the review! I have removed the performance claim in the latest commit.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
Removed mention of manual cloning in toSorted method explanation.
There was an error while loading. Please reload this page.
Successfully merging this pull request may close these issues.
There was an error while loading. Please reload this page.