compare-height-normalizing-solutions

JavaScript performance comparison

Test case created by Ryan

Preparation code

<div style="height: 200px;" class="foo">Foo</div>
<div style="height: 50px;" class="foo">Bar</div>
<div style="height: 100px;" class="foo">Baz</div>
    

Preparation code output

<div style="height: 200px;" class="foo">Foo</div> <div style="height: 50px;" class="foo">Bar</div> <div style="height: 100px;" class="foo">Baz</div>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
Ryan's way
window.addEventListener('load',

  function() {
    // declare the height array here since we need it
    // inside multiple functions
    var elementHeightArray = [],
      // convert the collection into an array so we can forEach() over it
      elementArray = [].slice.call(document.querySelectorAll('.foo'));
    elementArray.forEach(function(i) {
      elementHeightArray.push(i.clientHeight);
    });
    // get the tallest height
    var maxElementHeight = Math.max.apply(Math, elementHeightArray);
    // apply that height to all elements
    elementArray.forEach(function(i) {
      i.style.height = maxElementHeight + "px";
    });
  });
pending…
Yury's way
window.addEventListener('load', function() {
  var max = 0,
    elements = document.querySelectorAll('.foo');

  [].reduce.call(elements, function(prev, item) {
      max = Math.max(item.clientHeight, max);

      return function(height) {
        prev(item.style.height = height);
      };
    }, function() {})(max + 'px');
});
pending…

Compare results of other browsers

Revisions

You can edit these tests or add even more tests to this page by appending /edit to the URL.

0 Comments

Foo
Bar
Baz