Optimisation #1 Javascript : Mise en cache de la longueur d’un array

Pour optimiser un peu plus son code Javascript (pour ceux qui n’appliquent pas encore cette astuce bien sûr), je vais partager une petite best practice lors de l’itération d’une boucle for.

La boucle for « normale » ressemble à ça…
BAD

[javascript]
var tab = [‘element1’, ‘element2’, ‘element3’];
for (var i= 0 ; i<tab.length ; i++) {
// le reste du code
}
[/javascript]

Dans le code ci-dessus on constate qu’à chaque itération, la taille du tableau (tab) est recalculée via la commande tab.length. Mauvais!
Le but est de calculer cette longueur qu’une seule fois :
GOOD

[javascript]
var tab = [‘element1’, ‘element2’, ‘element3’];
for (var i= 0, iLen = tab.length ; i<iLen ; i++) {
// le reste du code
}
[/javascript]

Dans le code ci-dessus, on constante donc que la longueur est calculée la première fois et qu’elle est ensuite stockée dans la variable iLen.

Remarque
Pour des tableaux avec seulement quelques éléments l’optimisation ne se fait pas ressentir mais quand on travaille sur des milliers de lignes c’est plutôt pas mal!

43 Comments Optimisation #1 Javascript : Mise en cache de la longueur d’un array

  1. Heike

    Hi there! I know this is kind of off topic but I
    was wondering which blog platform are you using for this website?
    I’m getting tired of WordPress because I’ve had issues with hackers and I’m looking at alternatives for another platform.
    I would be awesome if you could point me in the direction of a good platform.

    Reply
  2. backlink seo

    Hi! This is kind of ߋff topic but I need some help from an estaƅlished blog.
    Is it tough to set up your own blog?I’m not very techincal but I can figure things out pretty quick.
    I’m thinking abouսt setting up my own but I’m not sure where to
    begin. Do you have any points or suggestions? Thanks

    Reply
  3. Richard

    Wow, wonderful blog layout! How long have you been blogging for?
    you make blogging look easy. The overall look of your website is great, as well as the content!

    Reply
  4. eBay music promo code

    217990 551812Your writing is fine and gives food for thought. I hope that Ill have far more time to read your articles . Regards. I wish you that you regularly publish new texts and invite you to greet me 225692

    Reply
  5. köpa sprutor

    879647 595618Hello, Neat post. There is an issue along with your web site in internet explorer, might test thisK IE nonetheless may be the marketplace chief and a big section of men and women will pass over your superb writing due to this dilemma. 425891

    Reply
  6. Chase

    Hey there! Would you mind if I share your blog with my zynga group?
    There’s a lot of people that I think would really enjoy your content.
    Please let me know. Thank you

    Reply
  7. Maryjo

    It’s remarkable to go to see this website and reading the views of all colleagues
    regarding this post, while I am also eager of getting familiarity.

    Reply
  8. instagram takipçi satın al

    Heya this is kinda of off topic but I was wanting to know if blogs use WYSIWYG editors
    or if you have to manually code with HTML. I’m starting
    a blog soon but have no coding experience so I wanted to get guidance from someone with experience.
    Any help would be greatly appreciated!

    Reply
  9. instagram takipçi satın al

    naturally like your website however you need to take a look
    at the spelling on several of your posts. Many of them are rife with
    spelling problems and I to find it very troublesome to tell the truth on the other hand I’ll
    certainly come back again.

    Reply
  10. instagram takipçi satın al

    We are a bunch of volunteers and starting a brand new scheme in our community.
    Your website offered us with helpful information to work on. You have done a
    formidable activity and our whole neighborhood
    will probably be grateful to you.

    Reply
  11. instagram takipçi satın al

    This is the right website for anybody who would like to understand this topic.

    You know so much its almost hard to argue with you (not that I personally would want to…HaHa).
    You certainly put a brand new spin on a subject that has been discussed for many years.
    Wonderful stuff, just great!

    Reply
  12. instagram takipçi satın al

    Great blog! Do you have any helpful hints for aspiring writers?
    I’m planning to start my own site soon but I’m a little lost on everything.

    Would you propose starting with a free platform like WordPress or
    go for a paid option? There are so many choices out there that I’m completely overwhelmed ..
    Any suggestions? Thank you!

    Reply
  13. pussy888 casino

    Do you have a spam problem on this site; I also am a blogger, and I was wanting to know your situation; many of us have created some nice procedures
    and we are looking to trade techniques with others,
    why not shoot me an email if interested.

    Here is my web blog: pussy888 casino

    Reply
  14. bet on sports online

    Woah! I’m really enjoying the template/theme of this website.
    It’s simple, yet effective. A lot of times it’s very difficult to get that “perfect balance” between user friendliness and appearance.

    I must say you have done a superb job with this.
    Additionally, the blog loads very fast for me on Safari.
    Exceptional Blog!

    Reply
  15. xe88sg.com

    Hey I am so grateful I found your site, I really found you by accident, while I was browsing on Bing for something else,
    Anyhow I am here now and would just like to say many thanks for a tremendous
    post and a all round thrilling blog (I also love the theme/design), I don’t have time
    to read through it all at the moment but
    I have bookmarked it and also added your RSS feeds,
    so when I have time I will be back to read much more, Please do
    keep up the awesome job.

    Here is my website wm casinoapk ios (xe88sg.com)

    Reply
  16. download game ok388

    An impressive share! I’ve just forwarded this onto a coworker who had been conducting a
    little research on this. And he in fact ordered me lunch simply because I stumbled upon it for him…
    lol. So let me reword this…. Thanks for the meal!! But yeah, thanx for spending time to talk about this topic here on your site.

    Here is my blog post; download game ok388

    Reply
  17. vampire slots

    We’re a group of volunteers and opening a new scheme in our community.

    Your website offered us with valuable information to work on. You’ve done an impressive
    job and our entire community will be grateful to you.

    Reply
  18. Eleanore

    Hola! I’ve been following your weblog for a while now and finally got the courage to go ahead and give you a shout out from
    Atascocita Tx! Just wanted to tell you keep up the good job!

    Reply

Leave a Reply

Your email address will not be published.