#336 ✓resolved
Nick Stakenburg

Faster document.viewport.get[Dimensions|Height|Width]

Reported by Nick Stakenburg | September 10th, 2008 @ 09:02 PM | in 1.6.1

In #335 kangax mentioned we could do less Opera sniffing in document.viewport.getDimensions.

Sniffing for Opera and WebKit on every call to getDimensions can be avoided. document.viewport.getHeight|getWidth could also do 50% less work. These changes would turn document.viewport into something like this:


document.viewport = {
  getDimensions: function() {
    return { width: this.getWidth(), height: this.getHeight() };
  } // , ...
};

(function(v) {
  var B = Prototype.Browser, doc = document,
   element, property = {};

  function define(D) {
    element = element || (B.WebKit && !doc.evaluate ? doc :
     B.Opera && parseFloat(window.opera.version()) < 9.5 ? doc.body :
     doc.documentElement);
    property[D] = 'client' + D;

    v['get' + D] = function() { return element[property[D]] };
    return v['get' + D]();
  }

  v.getWidth = define.curry('Width');
  v.getHeight = define.curry('Height');
})(document.viewport);

Comments and changes to this ticket

  • John-David Dalton

    John-David Dalton September 10th, 2008 @ 10:01 PM

    • State changed from “new” to “enhancement”
  • Juriy Zaytsev

    Juriy Zaytsev September 10th, 2008 @ 11:11 PM

    • Tag changed from dimensions, opera, webkit to dimensions, needs_patch, needs_tests, opera, webkit

    Nick, thanks for opening a separate ticket. The patch looks good, we just need tests for this.

  • Nick Stakenburg

    Nick Stakenburg September 11th, 2008 @ 02:06 AM

    Thanks, I'll add a patch for this later. Current unit tests should cover this though, I'll run them when I've got a patch to make sure nothing breaks.

  • Nick Stakenburg

    Nick Stakenburg September 11th, 2008 @ 09:00 PM

    • Title changed from “improving document.viewport.*” to “Faster document.viewport.get[Dimensions|Height|Width]”
    • Tag changed from dimensions, needs_patch, needs_tests, opera, webkit to dimensions, needs_tests, opera, webkit

    Added patch. I've also optimized things a bit further using JDD's lazy approach (from #337) combined with some caching to sniff only once when required.

  • Nick Stakenburg

    Nick Stakenburg September 19th, 2008 @ 12:44 PM

    • Tag changed from dimensions, needs_tests, opera, webkit to dimensions, needs_tests, opera, webkit

    Updated the patch after discussion with JDD and Garret Smith in #337 let to further optimization. At the moment I prefer this approach over the no-sniff one in #337 since the testing there seems a bit to experimental. This patch doesn't add quirksmode support but Prototype never supported that anyway so it's not required. Sticking with a sniff since it has proven to be stable. Updated the code above to reflect changes and added benchmarks.

    *Results are based on 10000 iterations.

    
    Safari 2
    Width (1937ms -> 82ms): 23.62x faster
    Height (1971ms -> 82ms): 25.05x faster
    Dimensions (2017ms -> 173ms): 11.66x faster
    
    Safari 3.1.2
    Width (437ms -> 32ms): 13.66x faster
    Height (421ms -> 32ms): 13.16x faster
    Dimensions (484ms -> 47ms): 10.30x faster
    
    Firefox 2
    Width (1195ms -> 117ms): 10.21x faster
    Height (1158ms -> 113ms): 10.25x faster
    Dimensions (1085ms -> 239ms): 4.54x faster
    
    Firefox 3
    Width (467ms -> 52ms): 8.98x faster
    Height (465ms -> 54ms): 8.61x faster
    Dimensions (476ms -> 122ms): 3.90x faster
    
    Chrome (build 1798)
    Width (188ms -> 19ms): 9.89x faster
    Height (193ms -> 22ms): 8.77x faster
    Dimensions (198ms -> 31ms): 6.39x faster
    
    IE6
    Width (1261ms -> 111ms): 11.36x faster
    Height (1331ms -> 121ms): 11.00x faster
    Dimensions (1241ms -> 191ms): 6.50x faster
    
    IE7
    Width (1137ms -> 119ms) : 9.55x faster
    Height (1145ms -> 121ms): 9.46x faster
    Dimensions (1115ms -> 211ms) : 5.28x faster
    
  • Nick Stakenburg
  • Filippo Buratti

    Filippo Buratti October 16th, 2008 @ 11:45 PM

    Thanks thanks thanks, to me this patch works great!

  • Andrew Dupont

    Andrew Dupont October 17th, 2008 @ 12:29 AM

    • Milestone set to 1.6.1

    Great work, Nick. Can you do me a favor and verify that this patch works for you? I rewrote your code to be a bit more Prototype-like (also so that I could document the sniffs that you do, since we're starting to do that as a matter of practice).

    This causes all tests to pass for me in Safari, but I don't have 2.0 installed on this machine.

  • Nick Stakenburg

    Nick Stakenburg October 17th, 2008 @ 01:45 AM

    Thanks Andrew, your patch works on Safari 2 and the other browsers mentioned in the benchmarks above (+Opera). I like the documentation friendly style, perhaps trim of a few curly branches to make it even more Prototypish?

    Great to see this on the 1.6.0.4 milestone, it'll give a nice boost.

  • GitHub Robot

    GitHub Robot December 16th, 2008 @ 03:40 AM

    • State changed from “enhancement” to “resolved”

    (from [c3c953363b178ca2b0ee270b9c081858658693f6]) Optimize document.viewport.get(Dimensions|Width|Height). [#336 state:resolved] http://github.com/sstephenson/pr...

  • Tobie Langel

    Tobie Langel July 24th, 2009 @ 02:18 AM

    • Tag changed from dimensions, needs_tests, opera, webkit to needs_tests, opera, section:dom, webkit

    [not-tagged:"dimensions" tagged:"section:dom" bulk edit command]

  • Tobie Langel

    Tobie Langel July 24th, 2009 @ 02:26 AM

    • Tag changed from needs_tests, opera, section:dom, webkit to missing:tests, opera, section:dom, webkit

    [not-tagged:"needs_tests" tagged:"missing:tests" bulk edit command]

  • Tobie Langel

    Tobie Langel July 24th, 2009 @ 03:36 AM

    • Tag changed from missing:tests, opera, section:dom, webkit to needs:tests, opera, section:dom, webkit

    [not-tagged:"missing:tests" tagged:"needs:tests" bulk edit command]

  • Tobie Langel

    Tobie Langel July 24th, 2009 @ 12:38 PM

    • Tag changed from needs:tests, opera, section:dom, webkit to needs:tests, section:dom
  • Tobie Langel

    Tobie Langel July 24th, 2009 @ 12:43 PM

    • Tag changed from needs:tests, section:dom to section:dom

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile ยป

The Prototype JavaScript library.

Shared Ticket Bins

Referenced by

Pages