Skip to content
Advertisement

Javascript performance issue with big data

I am filtering a list with javascript. My code works without issues but when the list const gets greater than 100 the browser freezes.

JavaScript

What am I doing wrong and how can I make this code working for a list of much more values (> 50.000).

Advertisement

Answer

See if this helps to begin with:

  • Move lower-casing filterVal out of the .filter loop. There’s no need to do that for each item in list.
  • Only write innerHTML once (instead of clearing it and then appending to it).
JavaScript

Going forward, you may want to think about a framework such as React (or something lighter-weight like Svelte or Mithril) instead of building your HTML by hand.

That is especially true since a customer named <script>alert("hello")</script> will presently cause havoc on your site. 🙂

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement