O'Reilly Forums: Javascript Optimization - O'Reilly Forums

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Javascript Optimization

#1 User is offline   AverageJake 

  • Active Member
  • PipPip
  • Group: O'Reilly Author
  • Posts: 36
  • Joined: 04-October 10
  • Gender:Male
  • Location:Seattle, WA

Posted 08 December 2010 - 03:37 PM

Last class Bryan mentioned a few JavaScript optimizations and that reminded me of this video. Hopefully some of you find it as interesting as I did.

http://blip.tv/file/2999333

**updated to correct the spelling of Bryan's name, sorry about that**
0

#2 User is offline   itanex 

  • Active Member
  • PipPip
  • Group: Members
  • Posts: 26
  • Joined: 23-February 10
  • Gender:Male
  • Location:Seattle, WA
  • Interests:Various Programming Topics and Approaches, Web Development and Standard and Practices

Posted 08 December 2010 - 09:49 PM

As promised, though a day late. The for loop optimization I mentioned at the end of class.

First here is a similar loop like you see everywhere:

CODE
for(var i = 0; i < list.length; i++)


Now this loop seems simple and that nothing can be done to make it faster. But the following are items that slow it down.

i++ explained:

i++ is the shorthand for i = i + 1. This means that the JIT creates an instance scope to find i + 1 then assigns the result to the assignment operator. What slows down the loop is the scope generation. Generally like this:

CODE
var i = 0;
while(i < 100){
    i = (function(i) { i = i +1; });  //[url="http://www.lmgtfy.com/?q=javascript+closures"]JavaScript Closures[/url]
}


Now ++i is different in the fact that it doesn't solve for the solution before assigning to i because it does it all in the same scope so it solves and assigns as if you did this loop:

CODE
var i = 0;
while(i < 100){
    i = i +1;
}


list.length explained:

It doesn't make sense when you just read and interpret this literally. But what happens really makes plenty of sense. When you request this "property" of the array, the JIT needs to calculate an value instead of reading a property.

Optimized:

for(var i = list.length -1; i >= 0; --i)

Explained:

Assigning the list value allows for a faster process because your computer can count backwards faster then it can forwards when it processes JavaScript.

Pre-decrementing I instead of post-decrementing is also faster since it is the faster of the two decrementing operators.

So overall, taking advantage of the computational powers of the microprocessor and providing the more efficient level of decrementation we have a loop that works for us in our animations instead of code that drags its feet when running.

~~ Bryan Wood
Please note that I composed this while drugged up on thera-flu so if i mistook something let me know.

0

#3 User is offline   3magic 

  • New Member
  • Pip
  • Group: Members
  • Posts: 1
  • Joined: 20-January 11

Posted 20 January 2011 - 11:38 PM

QUOTE (itanex @ Dec 8 2010, 09:49 PM) <{POST_SNAPBACK}>
As promised, though a day late. The for loop optimization I mentioned at the end of class.

First here is a similar loop like you see everywhere:

CODE
for(var i = 0; i < list.length; i++)


Now this loop seems simple and that nothing can be done to make it faster. But the following are items that slow it down.

i++ explained:

i++ is the shorthand for i = i + 1. This means that the JIT creates an instance scope to find i + 1 then assigns the result to the assignment operator. What slows down the loop is the scope generation. Generally like this:

CODE
var i = 0;
while(i < 100){
    i = (function(i) { i = i +1; });  //[url="http://www.lmgtfy.com/?q=javascript+closures"]JavaScript Closures[/url]
}


Now ++i is different in the fact that it doesn't solve for the solution before assigning to i because it does it all in the same scope so it solves and assigns as if you did this loop:

CODE
var i = 0;
while(i < 100){
    i = i +1;
}


list.length explained:

It doesn't make sense when you just read and interpret this literally. But what happens really makes plenty of sense. When you request this "property" of the array, the JIT needs to calculate an value instead of reading a property.

Optimized:

for(var i = list.length -1; i >= 0; --i)

Explained:

Assigning the list value allows for a faster process because your computer can count backwards faster then it can forwards when it processes JavaScript.

Pre-decrementing I instead of post-decrementing is also faster since it is the faster of the two decrementing operators.

So overall, taking advantage of the computational powers of the microprocessor and providing the more efficient level of decrementation we have a loop that works for us in our animations instead of code that drags its feet when running.

~~ Bryan Wood
Please note that I composed this while drugged up on thera-flu so if i mistook something let me know.



Hi,thanks very much for sharing the information here.Really a nice programming knowledge is provided by you specially about i++.I was not aware of it before.Thanks a lot again for sharing the information here.


Website Development Denver

Denver Web Designs

0

#4 User is offline   toupi2 

  • New Member
  • Pip
  • Group: Members
  • Posts: 1
  • Joined: 13-April 11
  • Gender:Male

Posted 13 April 2011 - 12:11 AM

very interesting post. thank you
0

#5 User is offline   Jamesbell 

  • New Member
  • Pip
  • Group: Members
  • Posts: 2
  • Joined: 13-June 11

Posted 13 June 2011 - 02:20 AM

@itnax:Thats perfect

Not only in Java script increment and decrement operators affects speed of executions and processing. Many developers missing this concept and face problems during development phase.

silverlight development
0

#6 User is offline   lrko85 

  • New Member
  • Pip
  • Group: Members
  • Posts: 3
  • Joined: 06-December 11

Posted 07 December 2011 - 01:50 AM

sorry i am not from the IT background but something i know about Java script to which i am sharing with you. Before submitting a question on optimization or scripting issues, please review the assumptions, conventions and nomenclature utilized throughout this document. You may also want to consult References 1 - 14, especially Andy King and Danny Goodman authority books, from which many answers have been adapted.

Andy King, Speed Up Your Site: Web Site Optimization, 1st Edition, (New Riders). This is a Must-Have/Must-Read web site optimization reference book. Mi Islita recommends this book to webmasters, web designers, optimizers and programmers. Unlike Danny Goodman's book, this book is not about how to learn JavaScript but about optimization techniques. The book touches many optimization fields such as HTML, XHTML, CSS, file size, graphic and sound files, JavaScript, meta tags, bandwidth usage, networking resources, etc. It is not a book for beginners.
Danny Goodman, JavaScript Bible, 4th Edition; IDG Books Worldwide, Inc. As its name indicates, this is The Official Bible of all JavaScripters. Mi Islita highly recommends this book. Unlike Andy King's book, this book is not about optimization but about JavaScript programming. Considered by many as The Reference, this is a book for beginners and advanced programmers. Like the good wine, it gets better with each edition.
0

#7 User is offline   cornerslice 

  • Active Member
  • PipPip
  • Group: Members
  • Posts: 10
  • Joined: 07-December 11
  • Gender:Male
  • Location:Toronto, Canada
  • Interests:Improving http://icandrive.ca, paintball and BF3

Posted 07 December 2011 - 09:35 AM

I prefer using jquery as it is optimized to begin with.

- Steve
http://icandrive.ca
0

#8 User is offline   Malachi21 

  • New Member
  • Pip
  • Group: Members
  • Posts: 1
  • Joined: 08-December 11

Posted 08 December 2011 - 10:08 PM

I've started work recently at a new company and they have an existing application with 1000s of lines of Javascript code. The baseline contains dozens of JS files with easily over 10,000 custom lines of code, they also use multiple 3rd party libraries such as Jquery, Livequery, JQTransform and others. One of the major complaints they have been receiving from users is the slowness of the client side operation of the site. I've been tasked with optimizing and improving the performance of the JS. My first step will be obviously to move forward to the newest Jquery library, and incorporate JSMin into the build process.
0

#9 User is offline   jacob001 

  • New Member
  • Pip
  • Group: Members
  • Posts: 1
  • Joined: 15-January 12
  • Gender:Male
  • Location:Ahmedabad, Gujarat, India
  • Interests:Internet surfing, watching movies

Posted 18 January 2012 - 10:08 PM

Hello,

Thanks for the sharing post.

This is the such a nice informative post.

Thanks & Regards,
Jacob
Web Development Company
http://www.esparkinfo.com/
0

#10 User is offline   Handyortung 

  • New Member
  • Pip
  • Group: Members
  • Posts: 1
  • Joined: 13-March 12
  • Gender:Male
  • Location:Messedamm, Dresden
  • Interests:Reading, Browsing

Posted 13 March 2012 - 04:17 PM

@AverageJake, thanks for share a wonderful vireo on Thomas Fuchs: Extreme JavaScript Performance. V8, its really works in Chorome browser what he describes. Is different browser has different effects on that? Thanks.
Handy Ortung
SEO Firma
http://smartseosolutions.de
0

#11 User is offline   arunnayak 

  • Active Member
  • PipPip
  • Group: Members
  • Posts: 20
  • Joined: 12-June 12

Posted 12 June 2012 - 03:54 AM

javascript is a client-side scripting and you can make your application dynamic and active,

String concatenation causes major problems with Internet Explorer 6 and 7 garbage collection performance. Although these issues have been addressed in Internet Explorer 8 -- concatenating is actually slightly more efficient on IE8 and other non-IE browsers such as Chrome -- if a significant portion of your user population uses Internet Explorer 6 or 7, you should pay serious attention to the way you build your strings.
0

#12 User is offline   Garthwillson 

  • New Member
  • Pip
  • Group: Members
  • Posts: 1
  • Joined: 28-February 13

Posted 28 February 2013 - 04:21 AM

Hi,
It's a good technologies for developer. JavaScript easier means for programmers to develop such dynamic interfaces was needed.JavaScript allow for easier integration of JavaScript with other web development technologies

Web Devlopment

This post has been edited by Garthwillson: 28 February 2013 - 04:22 AM

0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users