r/javascript • • Jul 28 '15

I'm all in.

Post image
746 Upvotes

120 comments sorted by

View all comments

Show parent comments

0

u/[deleted] Jul 29 '15

[deleted]

9

u/Klathmon Jul 29 '15

Map(), Set()

Also if your application is limited by the performance of the data structures then don't write it in JS. (Unless you are just assuming it won't be fast enough, in which case actually test it first!)

Use the tool that's best.

0

u/[deleted] Jul 29 '15

[deleted]

2

u/spinlock Jul 29 '15

It's too bad you can't extend js in c or something. When I want a really tight loop in ruby, I'll write write the function in c and compile it into ruby. That way you can optimize the code that needs it and still have the rest of the app written at a higher level.

1

u/Klathmon Jul 29 '15

You can on the server side, browser side is getting there.

With ES6 features like set, map, and typed arrays (basically near-raw memory blocks) you can get pretty insane speed from the browser too.

1

u/spinlock Jul 29 '15

I'm just upgrading my ember app to ES6 but its transpiled down to ES5. Won't that kill any performance?

Also, I've never had an easy time maintaining custom functions when I control the server. I can't imagine how had it would be on someone else's browser. But, hey, who doesn't like a challenge :)

2

u/Klathmon Jul 29 '15 edited Jul 29 '15

Yes and no.

Most JS engines will already use the same "backend" for stuff like Map and Set of you ONLY stick to some of those features, so by letting the features enforce it, the transpiler can generate code that helps "hint" the VM about what you are doing so it can choose the best underlying tech and get the best performance.

On the other hand many new ES6 features are very slow right now because they are "not optimized". Scroll down to the list of unsupported syntax here to get an idea of what will cause slower JS code, then review your transpiler output every now and then to see if any of those are creeping into your code.

2

u/spinlock Jul 29 '15

Thanks. I'll keep that in mind.

1

u/Klathmon Jul 29 '15 edited Jul 29 '15

Well its much better today.

Also add in typed arrays and buffers in the browser as well. They are stupid fast when used correctly.

Also don't just assume that the data structures in JS can't keep up. It might take a bit more knowledge of how the underlying VMs work, but with some tuning its possible to get pretty damn fast speeds from normal JSON objects or arrays.

The VMs will do their best to use underlying performant structures when possible.

2

u/inspired2apathy Jul 29 '15

Wow, good to see things have gotten so much better. I still don't particularly care for the various JS inconguities/gotchas, but it's much more feature-complete than it used to be.

1

u/Ehdelveiss Jul 29 '15

I thought this was the purpose of JSON? Or am I misunderstanding?

5

u/jkoudys Jul 29 '15

You are, a little. There's two basic datastructures that nearly every js app is going to use: the Array, and the Object. An array is a little closer to its classic definition than, say, PHP's array is, but it's far from what an "array" is in C, and meant in general to represent any mutable dataset ordered by index. An object, conversely, has no predefined order, and it's just a hashmap (this is what JSON -- technically a separately defined format -- loads as). Even if you have an object {a: 'foo', b: 'bar', c: 'baz'}, it technically has no order, and without logic around it to enforce the behaviour, you can't rely on property "a" coming before property "b".

If you're in a language like C or Java, much of the syntax and libs are dedicated to defining data structures based on their low-level behaviour. The syntax in JavaScript is focused more on high-level behaviour: the logic that your datastructure should follow as observed by those calling it. So as a JavaScript developer, you can look at a fairly complex dataset defined in JSON, but it's not necessarily optimized to run very quickly for what you're doing.

I am noticing that this is changing quite a bit. In addition to the upcoming harmony datastructures /u/Klathmon mentions (Map and Set), there have also been TypedArrays out for a while. These look more like traditional arrays, which simply define an extent (over an arraybuffer), and then the Unit8Array or Float64Array, etc. you define simply defines how to iterate it and compute offsets within the buffer. I'd recently started using it to do some low-level work in rewriting a gameboy emulator as modern es6 JavaScript with ReactJS, and these TypedArrays were perfect for modelling a memory space with 8-bit registers: https://github.com/jkoudys/ReactGB/blob/master/js/utils/emulator/z80.js#L43

All that said, the v8 engine and its JIT can be downright magical, in both the best and worst possible ways. Sometimes it figures out exactly what you mean when you setup a more complex datastructure, and other times a tiny change takes efficient processing down to garbage. Probably the worst thing for it to figure out is in pruning trees and removing nodes -- downright trivial on a good C implementation, with simple pointers, but taking a large array (or even object) and deleting nodes can confuse the hell out of v8.

1

u/Ehdelveiss Jul 29 '15

Thank you for your detailed reply! That helps me understand much better :)

0

u/thebuccaneersden Jul 29 '15

asm.js

1

u/inspired2apathy Jul 29 '15

But now it looks like it'll be WebAssembly instead.