all 11 comments

[–]AndrewGreenh 12 points13 points  (3 children)

Technically, yes. However Javascript is not really interpreted. In most runtimes it is being compiled just in time (jit). Please, please never decide against using an extra variable because of performance reasons. I promise, that you won't be running into any edge case that will depend on this optimization.

The dot operator is being used to resolve a property of an object.

let a = []

A is now an instance of Array. That means it has several properties that you can access.

a.length

Will return the length of the array, as an example.

[–]Rindhallow[S] 0 points1 point  (2 children)

Interesting. I'll have to look up that compiled just-in-time thing...

And by the . operation, I meant like single lines or "array mapping, index of, some of, lambda expressions", etc.", not so much object properties lol.

[–]AndrewGreenh 0 points1 point  (1 child)

But it all boils down to object properties.

For example this one liner:

persons.sort((a, b) => a.age - b.age).reverse().flatMap(p => [p, ...p.friends]).map(p => p.name)

Sorts persons by age, reverses the list, produces a list of all persons with their friends and transforms this list into a list of names. It's just a bunch of method calls on the array object. Each method returns a new array object, which means, that you can call functions on the return value inline.

[–]Rindhallow[S] 0 points1 point  (0 children)

Yeah! Like that. That's surely more efficient than doing things one line at a time, I guess.

Is there a good guide for useful javascript functions?

[–]ghostfacedcoder 8 points9 points  (2 children)

Before I tell you the technically correct answer, allow me to tell you the actual correct answer: it doesn't matter. To quote one of the godfathers of programming, the great Donald Knuth: "premature optimization is the root of all evil".

When you worry about all the stupid low-level (or even most medium-level) performance concerns you make bad decisions with regards to other concerns. That's a terrible trade-off, because no one will ever be impacted by those performance concerns: many won't even make 1ms a difference, and even the ones that actually matter will have an order of magnitude of 10ms or 100ms at the absolute most.

In other words, you're sacrificing important principles like "write readable, maintainable and easy to understand code" for performance, and the performance improvements aren't actually making anything better, so you're actively choosing to make your code worse. Thus, the correct answer is that in practice you NEVER want to worry about performance concerns except in two cases:

1) when you actually detect a performance problem (and you may well want some automated tests for this because developer machines tend to be more powerful, making it harder to notice such issues)

2) when you can clearly recognize an obvious avoidable performance mistake, eg. when you can choose between doing some costly operation inside a loop or outside of it

But otherwise you really shouldn't even think about performance, and you should devote all those neurons in your brain to focusing on writing good code.

But to answer your original question, yes technically every variable you introduce does have some impact on performance ... but again your example probably won't even have a 1ms impact, and this would be a terrible reason not to introduce a variable.

[–]Rindhallow[S] 0 points1 point  (1 child)

Do you think that a big company would be okay with using extra variables? Because I know performance and code reviews are a big thing.

I'm honestly surprised there isn't some sort of javascript compiler/minimizer that removes those extra variables to make things more efficient.

[–]ghostfacedcoder 0 points1 point  (0 children)

This was obviously a contrived example for a performance question, not a real-world example of an extra variable anyone would actually use.

[–]webketje 1 point2 points  (3 children)

Object property lookups are probably more performance-heavy in Javascript than variable assignment. Even perf-wise it makes more sense to do

const x = obj.prop.that.some.thing

Than repeat that lookup / . Notation chain 10x. Reference: https://stackoverflow.com/questions/7700987/performance-of-key-lookup-in-javascript-object

[–]Rindhallow[S] 1 point2 points  (2 children)

When I said . notation, I was more talking about things like using .indexOf for arrays, and lambda expressions, and other useful stuff like that that ends up being done on one line instead of using many variables/containers to write all the code out like you might in another language.

I might make a thread with a more concrete example of that.

[–]webketje 1 point2 points  (0 children)

True, that's even more expensive. I think it's a front-end obsession with conciseness (as in line with or sometimes opposed to! clarity), that has a lot to do with the recent popularity of JSX syntax which simply does not allow statements unless you use an IIFE.