How toSorted() Crashed Our Web App - And How We Fixed It

A few weeks ago, we ran into an unexpected issue that caused our Listing Availabilty page to crash for some landlords. This is the story of how a modern convenience bit us in the back, how we identified the culprit, and what we did to fix it.

What Happened

In modern JavaScript, Array.prototype.toSorted() is a safer alternative to Array.prototype.sort(). Unlike sort(), which mutates the original array, toSorted() returns a new, sorted copy of the array.

That immutability is a big win for avoiding side effects in functional programming. Naturally, we introduced it in a recent pull request.

We had a block of code that looked like this:

const sortedBlocks = blocks?.toSorted(sortByCreatedDateDescending);

But here’s the catch:

  • toSorted is part of the ECMAScript 2023 specification.

  • Many older browsers, and even some users on slightly outdated versions of Chrome, Firefox or Safari, don’t support it.

  • So when those users hit our page, their browsers threw an error and the page broke.

How We Discovered the Issue

Everything worked fine in development, staging, and production environments when we tried to reproduce it on our machines. This led us to question what could be peculiar to the landlord.

We decided to figure out the issue using Rollbar (our error monitoring tool); at first, I filtered using email:"..." to see the errors this specific user encountered within the timeframe the bug was reported and then I saw:

TypeError: e.toSorted is not a function

Digging deeper with Rollbar occurrences tab, I saw that about 25 other landlords face this issue which was supposedly meant to be low priority since we initially assumed based on the bug report that only one landlord experience it.

At this point, I suspected that toSorted() might be the issue. So I turned to my go-to compatibility checker: Can I use. A quick search for toSorted confirmed my suspicion:

Can I use

❌ Not supported in versions of Chrome < 110, Safari < 16.0, and Firefox < 115.

As shown under Rollbar Detail section, the affected users had one thing in common: older browsers. The reported landlord was on Chrome 109.0.0.

Old browser

Well, that explains the bug.

The modern toSorted() method was simply not available in older browsers. Unlike sort(), which has been supported since the earliest days of JavaScript, toSorted() is relatively new — and using it directly will throw a runtime error in unsupported environments.

The Fix

To fix the issue, I decided to revert to the good old sort() method, but with a defensive approach to preserve immutability:

// ❌ Crashes on old browsers
const sortedBlocks = blocks?.toSorted(sortByCreatedDateDescending);

// ✅ Works everywhere
const sortedBlocks = [...blocks].sort(sortByCreatedDateDescending);

By spreading the blocks array first, I avoid mutating the original array, mimicking the behavior of toSorted() — but using syntax that works across all browsers.

Once I deployed this fix, I used Browserstack to verify that the page doesn't crash anymore, and the Rollbar reports ceased.

Lessons Learned

Here are a few takeaways from this experience:

  • Always check browser compatibility when using newer JavaScript features. Can I use is your friend.

  • Don’t assume your development or staging environment matches production usage. You might be running the latest browser; your users might not.

  • Error monitoring tools are lifesavers. Without caniuse.com and Rollbar logs, this would’ve been much harder to debug.

  • When in doubt, use feature detection or fallback logic to gracefully degrade functionality.

Final Thoughts

Modern JavaScript brings tons of new features that make our code cleaner and more maintainable. But with great syntax comes great responsibility.

In our case, the shiny new toSorted() method was the culprit behind a pretty serious production issue — and a good reminder that backward compatibility still matters.

So the next time you're tempted to reach for a new feature, remember to ask: Can I use this?

Article Tag  Post Mortem

Share this article:

Stay Updated

More Articles


I write about accessibility, performance, JavaScript and workflow tooling. If my articles have helped or inspired you in your development journey, or you want me to write more, consider supporting me.

This site uses cookies to personalize content, show ads, and analyze traffic. See our Privacy Policy.