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 8596831
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T00:46:26+00:00 2026-06-12T00:46:26+00:00

I just heard from co-workers that using the length property directly in the validation,

  • 0

I just heard from co-workers that using the length property directly in the validation, has lower performance than assigning the value to a variable:

for(var i:int=0;i<array.length;i++)
   trace(String(i));

for(var i:int=array.length-1;i>-1;i--)
   trace(String(i));

They actually claim that, the second loop will iterate over the array “up to 90% faster”, is any of this true??

This question can apply for any language, but im only interested on AS3 behavior for this, especially on ArrayCollections.

  • 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-12T00:46:27+00:00Added an answer on June 12, 2026 at 12:46 am

    The reason for this issue is a lot more interesting than you would expect.

    Examine the following code, it includes seven tests:

    Here are the results:

    1. 864 – compare with literal constant int
    2. 866 – compare with int
    3. 1358 – compare with vector length (size fixed)
    4. 1376 – compre with vector length (size dynamic)
    5. 3159 – compare with object member
    6. 3152 – compare with static object member
    7. 11855 – compare with array length

    Why do you suppose the last one is so terribly slow when compared to the rest?
    It is not because the Array re-calculates the length each time, that would be silly.

    Read this:

    length property: A non-negative integer specifying the number of elements in the array. This property is automatically updated when new elements are added to the array. When you assign a value to an array element (for example, my_array[index] = value), if index is a number, and index+1 is greater than the length property, the length property is updated to index+1.

    The reason is in the implementation

    //Implementation
    public function get length():uint
    public function set length(value:uint):void
    

    The other six tests use a regular public member of a class.
    Array’s use getter and setter functions to retrieve the length value.
    If you continue to elaborate the test, you will see function calls cost precious time.
    When you need more performance, you sometimes have to rely on inline code.
    That is is true almost every time. That is because the processor has to ‘jump’ to a different area in the code, create a new scope and some additional reasons.

    Why is inlining considered faster than a function call?

    If you check the length implementation for vector, you will see it is just a public member unlike array (getter and setter) functions. Getters and Setters are better for extensibility, they can make your life a lot easier if you decide to inherit from a class, setters can also prevent certain errors by checking the values. Nothing beats a public property for speed.

    package regression 
    {
        import flash.display.Sprite;
        import flash.utils.getTimer;
        /**
         * ...
         * @author Arthur Wulf White
         */
        public class Check_Loop_Speed_1 extends Sprite
        {
            //BIG_NUMBER == 100,000,000
            public function Check_Loop_Speed_1() 
            {
                var i : int = 0, j : int = 100000000, time : int = 0;
                var vector: Vector.<Boolean> = new Vector.<Boolean>(100000000, true),
                    vect2 : Vector.<Boolean> = new Vector.<Boolean>(100000000),
                    obj : Object = new TestObject(),
                    arr : Array = new Array();
    
                arr.length = 100000000;
    
                //test  1
                time = getTimer();
                for (i = 0; i < 100000000; i++) { }
                trace(getTimer() - time);
    
                //test  2
                time = getTimer();
                for (i = 0; i < j; i++) { }
                trace(getTimer() - time);
    
                //test  3
                time = getTimer();
                for (i = 0; i < vector.length; i++) { }
                trace(getTimer() - time);
    
                //test  4
                time = getTimer();
                for (i = 0; i < vect2.length; i++) { }
                trace(getTimer() - time);
    
                //test  5
                time = getTimer();
                for (i = 0; i < obj.val; i++) { }
                trace(getTimer() - time);
    
                //test  6
                time = getTimer();
                for (i = 0; i < obj.val2; i++) { }
                trace(getTimer() - time);
    
                //test  7
                time = getTimer();
                for (i = 0; i < arr.length; i++) { }
                trace(getTimer() - time);
            }
    
        }
    
    }
    
    class TestObject
    {
        public var      val     : uint = 100000000;
        public const    val2    : uint = 100000000;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've just upgraded from MRI-1.8.7 to MRI-1.9.1 and heard about this lighthouse ticket which
Is it possible? From what I have heard it is. I wouldn't mind just
I just heard about that x mod (2^32-1) and x / (2^32-1) would be
I have just heard that - push notification is possible in iPhone I need
I have just heard that the iphone cannot do double natively thereby making them
I've heard before that modules are just classes too. I have a few situations,
Just curious. I heard something about the new G1GC addressing that latency problem. I
I just heard Microsoft Expressions studio from my project manager and he told me
I heard from someone that DSL is really powerful in some specific fields. So
I just heard that spawn your own threads in JavaEE container is a bad

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.