DEV Community

loading...

Discussion on: JavaScript does not cache array.length

Collapse
akashkava profile image
Akash Kava Author

Well length is stored as a special field on object instead of key/value pair, it is kind of shortcut property. Analyzing escaping etc, all require some logic.

Profiler is not an ideal, writing better code is never harmful, it is good practice. We usually have millions of lines of code and you can't sit and profile thousands of different patterns unless you are paid millions of dollars for applications.

Collapse
pentacular profile image
pentacular

Length may be stored as a special field on some implementations, but it isn't required to be the case -- be careful not to confuse your favorite implementation with the language itself.

The problem with 'writing better code' is that what is better depends on many variables.

A profiler is a way to test what is better, and how much better, in a particular environment and with a particular workload.

This allows you to make decisions based on actual data rather than guessing -- it's often the case that the optimizations you think ought to matter just aren't important at all for how the code performs.

In the absence of profiling, I'll always recommend maximizing readability, since that's the most constant cost.