Reputation: 57971
I'm so curious because I'm maintaining a 3d engine written in Javascript.
parseInt(el.style.width)
or el.offsetWidth
?parseInt(el.offsetWidth)
?getElementById()
or childNodes[]
?new Image()
or appendChild('img')
?Thank you!
Upvotes: 0
Views: 629
Reputation: 50137
You've got quite a bit of confusion here.
1. Which is faster, parseInt(el.style.width) or el.offsetWidth?
el.style.width
gives you the width set by javascript or style attribute in the markup.
offsetWidth
gives you the current width of the element (including borders).
2. Should I write
parseInt(el.offsetWidth)
?
No, offsetWidth
returns a number. But you should specify the radix whenever you use parseInt
!
3. Which is faster,
getElementById()
orchildNodes[]?
If you got the parent use the childNodes
. If you got the id, you can use byId. Performance will depend on the browser (test).
4. Which is faster,
new Image()
orappendChild('img')
?
If you want to force the browser to download a resource, use new Image()
. If you want to add an image to the layout use appendChild(IMAGE_ELEMENT)
5. Give me please a link to a splendid Javascript PERFORMANCE guide
I rather leave you with this link: http://jsperf.com/, and let you do A/B testing.
Upvotes: 4
Reputation: 86902
Here are a couple of sites regarding Javascript preformance.
Really old site..but provides some insight.
http://home.earthlink.net/~kendrasg/info/js_opt/jsOptMain.html
Reference to a book
Upvotes: 0