DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
Building Scalable Real-Time Apps with AstraDB and Vaadin
Register Now

Trending

  • Using OpenAI Embeddings Search With SingleStoreDB
  • Designing a New Framework for Ephemeral Resources
  • A Data-Driven Approach to Application Modernization
  • Effective Java Collection Framework: Best Practices and Tips

Trending

  • Using OpenAI Embeddings Search With SingleStoreDB
  • Designing a New Framework for Ephemeral Resources
  • A Data-Driven Approach to Application Modernization
  • Effective Java Collection Framework: Best Practices and Tips
  1. DZone
  2. Data Engineering
  3. Data
  4. [JavaScript] Shoud I have to cache my array's length?

[JavaScript] Shoud I have to cache my array's length?

David Catuhe user avatar by
David Catuhe
·
Jan. 25, 15 · Interview
Like (0)
Save
Tweet
Share
12.76K Views

Join the DZone community and get the full member experience.

Join For Free

Happy new year!

To start this promising year, I would like to discuss about an interesting topic I saw on Twitter (@deltakosh if you want to discuss). The discussion was about array’s length access during a loop.

Simply put, should I use this:

var total = 0;
for (var i = 0; i < myArray.length; i++) {
    total += myArray[i];
}

Or that:

var total = 0;
for (var i = 0, len = myArray.length; i < len; i++) {
    total += myArray[i];
}

Should I use the .length property on every loop or should I cache it? Interesting question because almost all JavaScript code on the web have to use loops.

So pragmatically I created this small Jsperf: http://jsperf.com/arraylengthprecaching

And the result is self-explanatory:

clip_image002

Please do not be afraid by the absence of IE, this is due to some user agent sniffing changes we did.

First point that we can note: Results are slightly the same most of the time.

On desktop configuration, browsers are doing a great work and there is almost NO difference between our two options. We can even see some devices where cached version is slower than regular version.

For instance in latest version of IE (That you can test on Windows 10 Technical preview or using http://remote.modern.ie) , we started optimizing this recently by hoisting the length load out of the loop as you can see in this post. However, it is worth nothing that optimizations have limitations and don’t always kick in.

Mobile browsers do not have (yet) the array length hoisting optimization, so it is expected that length caching performs noticeably better in a micro benchmark like this one.

To sum up, when optimizing for performance, it’s almost always better to manually hoist such things, as it provides more information – it enforces the otherwise assumption that the JIT must make and guarantee, that the length does not change inside the loop, or that the loop does not care if the length does change. But on the other hand, if the code being written is not performance sensitive, it may not be worth going out of the way to optimize it, unless it is also done for readability or some other good reason (Vyacheslav Egorov wrote an excellent post on this topic)

Side note: I saw people arguing that accessing .length property can be longer because JavaScript arrays are stored internally as linked list. This is not true because the chunks of an array are stored as contiguous elements for random access within the chunks, but since JavaScript allows sparse arrays, sparse arrays store several chunks in a linked list, to balance access speed and memory usage.

Cache (computing) JavaScript

Published at DZone with permission of David Catuhe, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Trending

  • Using OpenAI Embeddings Search With SingleStoreDB
  • Designing a New Framework for Ephemeral Resources
  • A Data-Driven Approach to Application Modernization
  • Effective Java Collection Framework: Best Practices and Tips

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: