Algorithmic Complexity of Left-Pad (2016)
accidentallyquadratic.tumblr.com
accidentallyquadratic.tumblr.com
This is one of the more unnecessary pedantries I’ve ever seen, and that’s saying something on HN.
edit: It is not, however, an accurate distinction. https://news.ycombinator.com/item?id=28021135
“Clauses 18 through 28 define the ECMAScript standard library.”
—ECMA-262, 12th edition, June 2021, ECMAScript® 2021 Language Specification, §4.5
(Does the standard provide for a way not to run with the "standard library"? To opt for a different library of your choosing, for example, or no library at all? No? So it's a set of built-ins that are just an inescapable part of the language, then.)
And this is why we benchmark before optimizing
There's no reason to build the repeats up by doubling since the string concatenation of two same string is still linear time.
On that graph, 400,000 length required 2500 in time. If it's quadratic, 800,000 would require 10,000 in time. If you imagine the plot goes all the way to 800,000 - it's not hard to see that the line/curve hitting at 10,000.
Thoughts?
I think the line is drawn incorrectly. You can always fit a straight line into a curve if you zoom in enough which is what I think is happening here.
I have not tried it myself but I strongly suspect that if you try to fit a linear and quadratic regression line in that part of dataset - the quadratic line would have better fit(coefficient of determination) compared to linear regression line.
Anyway, the graph doesn't even try to suggest the performance behaviour shows a single linear relationship. The post (and the graph) explicitly says there seem two be two different linear segments. It's true that a single linear relationship would be a wrong model, but does that mean quadratic is right either?
I might be completely off but I wonder if the relationship could be generally linear but with different coefficients before and after the cutover point due to the data becoming larger than fits in the cache at once, or something.
You need 3 points for a fit a polynomial of degree 2.
I am not talking about the first linear segment at all. My concern is that the 2nd part is not linear. Drawing a line though eye balling is very error prone. That's point I am trying to make.
I'm a bit confused. Yes, you need (at least) three points to try and fit a polynomial of degree two. But both of your earlier examples of why the relationship could be quadratic consisted of two points.
I'd be more or less happy to accept that if the same quadratic formula worked (within a margin of error) for several other other, randomly chosen pairs of points in the graph.
The second segment seems roughly linear to me. There are a couple of points in the graph that are somewhat off, but it's (at least as shown in the graph) only a couple of points, and we don't know the accuracy of the measurements, so there could be some noise.
It's true that you can easily get spurious relationships by trying to see multiple different segments or otherwise zooming to an selected level. I'm just not really sure the points as a whole look like a quadratic curve either, and it's not impossible there could be multiple segments, as e.g. cache behaviour could potentially introduce such non-linearity in what could otherwise have linear performance.
It does look linear but that's the problems with eye ball testing. I think if you fit a quadratic, you'll start to see the gradual curve. Its will be a very zoomed in area in a parabola.
If I had more time I'd generate the dataset from the graph and try to fit linear and quadratic regression line and compare the models using coefficient of determination. That would provide some strong for/against evidence.
That's entirely possible.
Of course someone who knew how the algorithm worked (within the js engine) could just tell based on the code. :)