Once, about a year ago (maybe less), I was writing a GreaseMonkey/TamperMonkey script to replace all occurrences of a given string in a web page, and I was treating the list of as-of-yet unscanned nodes as a queue. This was before I learned that Array.prototype.shift is about as ineffecient as you can get for getting elements out of a large array in JS. Suffice to say I learned the hard way just how bad it gets
With the amount of work that things like replacers.filter(...) were doing, even after offloading the actual replacing work until after the loop, scanning the page kangax.github.io/compat-table/es6 (which at the time had roughly 194,000 nodes on it [and has only grown since then]) took anywhere between 60 and 90 seconds on page load.
After switching from a queue-based array to a stack-based array (by replacing nodes.shift() with nodes.pop(), the same page took about 4 seconds to scan.
I actually had an even worse-designed algorithm before this where I attempted to use nothing but Array.prototype methods and ended up iterating the result arrays multiple times, so the function to scan the page had something like a O(15n) runtime complexity. It was a very readable algorithm, though.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Once, about a year ago (maybe less), I was writing a GreaseMonkey/TamperMonkey script to replace all occurrences of a given string in a web page, and I was treating the list of as-of-yet unscanned nodes as a queue. This was before I learned that
Array.prototype.shift
is about as ineffecient as you can get for getting elements out of a large array in JS. Suffice to say I learned the hard way just how bad it getsWith the amount of work that things like
replacers.filter(...)
were doing, even after offloading the actual replacing work until after the loop, scanning the page kangax.github.io/compat-table/es6 (which at the time had roughly 194,000 nodes on it [and has only grown since then]) took anywhere between 60 and 90 seconds on page load.After switching from a queue-based array to a stack-based array (by replacing
nodes.shift()
withnodes.pop()
, the same page took about 4 seconds to scan.I actually had an even worse-designed algorithm before this where I attempted to use nothing but
Array.prototype
methods and ended up iterating the result arrays multiple times, so the function to scan the page had something like aO(15n)
runtime complexity. It was a very readable algorithm, though.