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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T06:45:36+00:00 2026-06-14T06:45:36+00:00

I am optimizing an HTML5 canvas animation. The structure looks like this: Animation =

  • 0

I am optimizing an HTML5 canvas animation.

The structure looks like this:

Animation = {
     //Cache constant variables
    cache : {
            var1 : 1,
            var2 : 2,
            var3 : 3
    },
    render : function(){
      //render to canvas

   }

}

Within the render function I am calling this.cache.var1, this.cache.var2 etc… multiple times throughout and performing various calculations on them.

What I want to know is if calling this repeatedly during a single loop carries significant overhead, if so how best to optimize this?

Should I simply store a local copy within the render loop (self=this;) and work from self, Should I look at minimizing the various repeated calculations, breaking them down into various parts, storing them as local variable within the loops and work from those?

Any opinions are welcome.

Note: I am aware this might be considered micro-optimization but when dealing with heavy animation processing I really would like to squeeze every last drop I can get.

Thanks in advance.

  • 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-14T06:45:38+00:00Added an answer on June 14, 2026 at 6:45 am

    Any opinions are welcome.

    You don’t want opinions, you want hard data! 🙂 To whit:

    http://jsperf.com/this-versus-closure

    Anim1 = {
       cache:  { var1:1, var2:2, var3:3 },
       render: function(){
         return this.cache.var1 + this.cache.var2 + this.cache.var3;
       }
    };
    
    AnimMaker = function(){
      var cache = { var1:1, var2:2, var3:3 };
      this.render = function(){
        return cache.var1 + cache.var2 + cache.var3;
      }
    };
    Anim2 = new AnimMaker;
    
    Anim3 = {
       cache:  { var1:1, var2:2, var3:3 },
       render: function(){
         var cache = this.cache;
         return cache.var1 + cache.var2 + cache.var3;
       }
    };
    // Now compare Anim1.render() vs Anim2.render() vs Anim3.render()
    

    Using a closure is slightly faster on Chrome, twice as slow on Firefox, about the same on IE9. Caching the reference to this.cache as var cache=this.cache improves speed ever-so-slightly on some browsers. However, the speed differences are not noticeable enough to make a noticeable impact in any code you’re writing. As shown in the results, my tests were running at hundreds of millions of lookups per second (50 million operations/second * 3 lookups per operation), much of that possibly dwarfed by the overhead of the function call.

    Focus on your canvas drawing and any looping algorithms, not individual lines of code.

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

Sidebar

Related Questions

I am optimizing sql query and I am welcoming advices how to improve this
I happens to read this http://code.google.com/speed/articles/optimizing-php.html It claims that this code $description = strip_tags($_POST['description']);
Strugle with optimizing this subquery that count item: SELECT @crewid:=crewid AS `crewid`, COUNT(id) AS
I use Asp.net 4 c# and Page OutPut Cache IN MEMORY for optimizing performances.
I need to run a flash animation, (swf file) for about 12 hours. This
I need some help optimizing a MySQL query or table When I run this
I read this on URL: http://docs.oracle.com/cd/E17952_01/refman-5.5-en/optimizing-myisam-bulk-data-loading.html If you lock table before you insert and
i was optimizing my html5 game engine for performance issues and i want to
I need help with optimizing some of my SQL queries. I'm not good in
The Situation: I'm optimizing a pure-java implementation of the LZF compression algorithm, which involves

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.