Dan
Dan

Reputation: 57971

Javascript performance question

I'm so curious because I'm maintaining a 3d engine written in Javascript.

  1. Which is faster, parseInt(el.style.width) or el.offsetWidth?
  2. Should I write parseInt(el.offsetWidth)?
  3. Which is faster, getElementById() or childNodes[]?
  4. Which is faster, new Image() or appendChild('img')?
  5. Give me please a link to a splendid Javascript PERFORMANCE guide

Thank you!

Upvotes: 0

Views: 629

Answers (2)

gblazex
gblazex

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() or childNodes[]?

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() or appendChild('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

John Hartsock
John Hartsock

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

http://oreilly.com/server-administration/excerpts/even-faster-websites/writing-efficient-javascript.html

Upvotes: 0

Related Questions