compare-height-normalizing-solutions

Benchmark created by Ryan on


Preparation HTML

<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

Ready to run.

Testing in
TestOps/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";
    });
  });
ready
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');
});
ready

Revisions

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