Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8475113
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T17:48:26+00:00 2026-06-10T17:48:26+00:00

In the book JavaScript Patterns Stoyan Stefanov he claims that the common way of

  • 0

In the book JavaScript Patterns Stoyan Stefanov he claims that the common way of looping in JavaScript

for (i = 0, max = myarray.length; i < max; i++) {
// do something with myarray[i]
}

can be optimized by using this pattern instead

for (i = myarray.length; i--;) {
// do something with myarray[i]
}

I found that to be interesting so I decided to test it in the real world by applying the technique to a performance intensive loop showed in this blog post about doing pixel manipulation with canvas. The benchmarks comparing the regular code with the “optimized” code can be seen here.

The interesting thing is that the supposedly optimized loop is actually slower than the regular way of looping in both Opera and Firefox. Why is that?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T17:48:28+00:00Added an answer on June 10, 2026 at 5:48 pm

    Most of the ways to try to optimise a loop comes from C, and a time when compilers where simpler and processors executed one instruction after the other.

    Modern processors run the code very differently, so optimising specific instructions doesn’t have the same effect now.

    For Javascript the changes are quite rapid now. It has gone from being interpreted to being compiled, which makes a huge performance difference. The compilers are very different between browsers, and they change with each new browser version, so something that is faster in one browser today, may be slower tomorrow.

    I have tested some different ways of optimising loops, and currently there is very little difference in performance: http://jsperf.com/loopoptimisations

    One thing that can be said for certain though, is that the regular way of writing loops is the most common, so that is what all compilers will be focusing on optimising.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently going through Stoyan Stefanov's book Object-oriented JavaScript and I've stumbled on an
I am doing some exercises in my object-oriented javascript book, I notice that this:
I'm learning JavaScript and there's an example in the book I'm using that I
According to JavaScript Patterns book (p. 79), this should work: var ob = {
The book Professional Javascript by Zakas says that 1) IE uses event bubbling 2)
Today, while I was randomly reading the JavaScript patterns O'Reilly book, I found one
Inside John Resig's book Pro Javascript techniques he describes a way of generating dynamic
I remember reading in Douglas Crockford's Javascript the Good Parts book that there is
This is the extend function from the book Pro JavaScript Design Patterns function extend(subClass,
I saw in Crockford's Book Javascript: The Good Parts that he does typeof comparison

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.