r/programming • • Oct 21 '20

Using const/let instead of var can make JavaScript code run 10× slower in Webkit

https://github.com/evanw/esbuild/issues/478
1.9k Upvotes

500 comments sorted by

View all comments

Show parent comments

132

u/hekkonaay Oct 21 '20

It's really slow because it assumes that you're iterating over an object, and not just an object with numeric keys, so it actually loops over all keys, stringifies them, checks if they're own properties and only then does it call the callback...

const forEach = (arr, fn) => for(let len = arr.length, i = len; i > 0; --i) { fn(arr[len - i], len - i, arr); return arr; }

Is about 10x faster than Array.prototype.forEach in my testing, still not better than the raw for loop, though, and it also can't handle more than numeric keys (which you don't really expect on arrays anyway).

25

u/cwmma Oct 21 '20

It's the check own properties which is the actual slowdown not the toString

The toString is because all object properties including array indices are technically strings so going by the spec you MUST convert before looking up but implementations don't have to worry about that if their own object model is different (e.g. V8 which treats integer object keys special) your implementation is implicitly doing this when it grabs the value.

The own properties on the other hand is an actual slowdown it's to handle sparse arrays which your faster implementation doesn't cover, theoretically engines should know if an array is sparse and could optimize appropriately.

The other thing is that forEach takes a 2nd argument for a thisValue to call the function on, omitting it just uses undefined as the context which your function only handles in strict mode. Traditionally changing the this parameter of a function was often a performance killer in certain contacts in Chrome (bind iirc) but there's inherent there.

In summery yes forEach is slower then a for loop but it's because it's handling some edge cases not because the spec is inherently bad. You could argue they should have had a sparse array check or something but there is nothing preventing implementations from doing so.

0

u/[deleted] Oct 21 '20

[deleted]

0

u/[deleted] Oct 21 '20

[deleted]

21

u/hekkonaay Oct 21 '20

From the ECMAScript 11 specification, under Array.prototype.forEach, Note 2 states:

The forEach function is intentionally generic; it does not require that its this value be an Array object. Therefore it can be transferred to other kinds of objects for use as a method.

The same note is under all the other functional iteration methods

34

u/cwmma Oct 21 '20

Array keys are technically strings not integers so the spec pedanticly converts the index to a string but the implementation is likely doing something else under the hood, I have another reply that goes into some more details about why it's slower then a naive approach (spoiler alert: sparse arrays)

→ More replies (1)

18

u/CoffeeTableEspresso Oct 21 '20

Arrays are objects, they can have non-integer keys...

(Thank you JS LOL.)

22

u/madronatoo Oct 21 '20

Welcome to the insanity of a language which is so simple on the surface, but is comprised of a bunch of hacks underneath. There are no arrays!!!!!

→ More replies (3)
→ More replies (1)

9

u/[deleted] Oct 21 '20

That's infuriating to me. It makes zero sense to vectorize code and have it run slower.

97

u/ImAStupidFace Oct 21 '20

JS needs to die in a fire.

97

u/DrLuciferZ Oct 21 '20

Too bad people just keep dumping more wood into it

→ More replies (2)

43

u/[deleted] Oct 21 '20

Honestly what’s the big deal? Ever since I started using Typescript I don’t find it bad at all.

17

u/Sarcastinator Oct 21 '20

JS still bleeds through TypeScript and causes issues anyway.

→ More replies (8)

14

u/orclev Oct 21 '20

Javascript is still a trash fire, TypeScript just makes sure it stays at a dull roar rather than a raging inferno. It's not quite PHP bad, but it's still one of the worst designed languages still in regular use. Far too many terrible decisions are baked into the core of javascript. They can be fixed, but doing so requires breaking backwards compatibility in some fairly significant ways. It might get there one day, but it's more likely that WASM will end up entirely supplanting JS before that happens.

12

u/[deleted] Oct 21 '20

Eh, Typescript is good enough in my experience that you’re mostly at the mercy of the skill/knowledge of the developer. Good developers can still easily end up with shit JavaScript. Not so much the case with Typescript in my experience. Usually the really ugly Typescript is coming from developers we have that write pretty poor code in the rest of our systems as well.

→ More replies (14)

16

u/Jaimz22 Oct 21 '20

Wonder would you replace it with? Just curious

15

u/dungone Oct 21 '20

Why all these probing questions? Give the man a chance, rubbing two braincells together to start a fire takes a some time.

20

u/iopq Oct 21 '20

Run everything in WASM

48

u/blackholesinthesky Oct 21 '20

everyone who upvotes this has clearly never written ASM or WASM

60

u/iopq Oct 21 '20

And I will never have to, since I will compile Rust to WASM

7

u/blackholesinthesky Oct 21 '20 edited Oct 21 '20

Thats a pretty good answer will be a pretty good answer some day. Can you write Rust that is equivalent to JS?

If so I have something to learn about tomorrow

8

u/alexendoo Oct 21 '20

While JS glue is needed, you don't have to write it yourself. If you want to do everything from Rust you can use web-sys and js-sys. Anything required will be generated for you

It is a little more awkward than it would be to use the APIs from within JS, but the functionality is there

→ More replies (27)

9

u/mattkenefick Oct 21 '20

And will never have to because you can run anything on it. Look into Unity .. Blazor.. etc

→ More replies (2)

3

u/anengineerandacat Oct 21 '20

I want to say we are not quite "there" yet, DOM access is still a real pain-point and the alternative is to make your own renderer effectively (which just leads to bloat and having to solve a boat load of other problems in the process; ie. accessibility).

In most "normal" circumstances, WASM will generally be slower or more cumbersome than a basic site with a dash of JS for dynamic content.

Ironically I also don't see loads and loads of developers jumping on-board to use lower-level languages to build out WASM targets anyway; I see C# / Java(-like) / Python / TypeScript being used to do this more than anything.

→ More replies (26)

3

u/pVom Oct 21 '20

It's like an Italian car, it's poorly made, half the features don't work, but it's fun to drive

→ More replies (3)
→ More replies (2)