border-width-option-parsing

JavaScript performance comparison

Test case created by kurkle

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  var b1= { left: 0, right: 5 };
  var b2 = 7;
  var bar1 = { _view: { width: 1, borderSkipped: false, _skipped: { } } };
  var bar2 = { _view: { _skipped: { b: true } } };
  var maxWidth = 2;
  var maxHeight = 3;
  
  
  
  function valueOrDefault(value, defaultValue) {
  		return typeof value === 'undefined' ? defaultValue : value;
  }
  
  function isObject(value) {
  		return value !== null && Object.prototype.toString.call(value) === '[object Object]';
  }
  
  function bound(min, value, max) {
  		return Math.max(min, Math.min(value, max));
  	}
  
  function isVertical(bar) {
  	return bar._view.width !== undefined;
  }
  
  function swap(orig, v1, v2) {
  	return orig === v1 ? v2 : orig === v2 ? v1 : orig;
  }
  
  function parseBorderSkipped(bar) {
  	var vm = bar._view;
  	var vertical = isVertical(bar);
  	var borderSkipped = valueOrDefault(vm.borderSkipped, vertical ? 'bottom' : 'left');
  
  	// For backward compatibility, 'bottom' for vertical and 'left' for
  	// horizontal actually mean 'start'. So 'bottom' / 'top'
  	// and 'left' / 'right' are swapped for negative bars.
  	if (vertical && vm.base < vm.y) {
  		borderSkipped = swap(borderSkipped, 'bottom', 'top');
  	}
  	if (!vertical && vm.base > vm.x) {
  		borderSkipped = swap(borderSkipped, 'left', 'right');
  	}
  	return borderSkipped;
  }
  
  function parseBorderSkipped2(bar) {
      var vm = bar._view;
      var vertical = isVertical(bar);
      var edge = vm.borderSkipped;
      var res = {};
  
      if (!edge) {
          return res;
      }
  
      if (vertical && vm.base < vm.y) {
          edge = swap(edge, 'bottom', 'top');
      }
      if (!vertical && vm.base > vm.x) {
          edge = swap(edge, 'left', 'right');
      }
  
      res[edge] = true;
      return res;
  }
  
  function parseBorderSkipped3(bar) {
      var vm = bar._view;
      var edge = vm.borderSkipped;
      var res = {};
  
      if (!edge) {
          return res;
      }
  
      if (isVertical(bar)) {
          if (vm.base < vm.y) {
              edge = swap(edge, 'bottom', 'top');
          }
      } else if (vm.base > vm.x) {
          edge = swap(edge, 'left', 'right');
      }
  
      res[edge] = true;
      return res;
  }
  
  function parseBorder1(value, bar, maxWidth, maxHeight) {
    var skipped = parseBorderSkipped(bar);
  	var t, r, b, l;
  	if (isObject(value)) {
  		t = +value.top || 0;
  		r = +value.right || 0;
  		b = +value.bottom || 0;
  		l = +value.left || 0;
  	} else {
  		t = r = b = l = +value || 0;
  	}
  
  	return {
  		top: skipped === 'top' ? 0 : t > maxHeight ? maxHeight : t < 0 ? 0 : t,
  		right: skipped === 'right' ? 0 : r > maxWidth ? maxWidth : r < 0 ? 0 : r,
  		bottom: skipped === 'bottom' ? 0 : b > maxWidth ? maxWidth : b < 0 ? 0 : b,
  		left: skipped === 'left' ? 0 : l > maxWidth ? maxWidth : l < 0 ? 0 : l
  	};
  }
  
  function parseBorder1b(value, bar, maxWidth, maxHeight) {
    var skipped = parseBorderSkipped(bar);
  	var t, r, b, l;
  	if (isObject(value)) {
  		t = +value.top || 0;
  		r = +value.right || 0;
  		b = +value.bottom || 0;
  		l = +value.left || 0;
  	} else {
  		t = r = b = l = +value || 0;
  	}
  
  	return {
  		top: skipped === 'top' || (t < 0) ? 0 : t > maxHeight ? maxHeight : t,
  		right: skipped === 'right' || (r < 0) ? 0 : r > maxWidth ? maxWidth : r,
  		bottom: skipped === 'bottom' || (b < 0) ? 0 : b > maxWidth ? maxWidth : b,
  		left: skipped === 'left' || (l < 0) ? 0 : l > maxWidth ? maxWidth : l
  	};
  }
  
  function parseBorder1c(value, bar, maxWidth, maxHeight) {
    var _skip = parseBorderSkipped2(bar);
  	var t, r, b, l;
  	if (isObject(value)) {
  		t = +value.top || 0;
  		r = +value.right || 0;
  		b = +value.bottom || 0;
  		l = +value.left || 0;
  	} else {
  		t = r = b = l = +value || 0;
  	}
  
  	return {
  		top: _skip.top || (t < 0) ? 0 : t > maxHeight ? maxHeight : t,
  		right: _skip.right || (r < 0) ? 0 : r > maxWidth ? maxWidth : r,
  		bottom: _skip.bottom || (b < 0) ? 0 : b > maxWidth ? maxWidth : b,
  		left: _skip.left || (l < 0) ? 0 : l > maxWidth ? maxWidth : l
  	};
  }
  
  function parseBorder1d(value, bar, maxWidth, maxHeight) {
    var _skip = parseBorderSkipped3(bar);
  	var t, r, b, l;
  	if (isObject(value)) {
  		t = +value.top || 0;
  		r = +value.right || 0;
  		b = +value.bottom || 0;
  		l = +value.left || 0;
  	} else {
  		t = r = b = l = +value || 0;
  	}
  
  	return {
  		top: _skip.top || (t < 0) ? 0 : t > maxHeight ? maxHeight : t,
  		right: _skip.right || (r < 0) ? 0 : r > maxWidth ? maxWidth : r,
  		bottom: _skip.bottom || (b < 0) ? 0 : b > maxWidth ? maxWidth : b,
  		left: _skip.left || (l < 0) ? 0 : l > maxWidth ? maxWidth : l
  	};
  }
  
  

};
</script>

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
1
parseBorder1(b1, bar1, maxWidth, maxHeight);
parseBorder1(b2, bar1, maxWidth, maxHeight);
parseBorder1(b1, bar2, maxWidth, maxHeight);
parseBorder1(b2, bar2, maxWidth, maxHeight);
pending…
1d
parseBorder1d(b1, bar1, maxWidth, maxHeight);
parseBorder1d(b2, bar1, maxWidth, maxHeight);
parseBorder1d(b1, bar2, maxWidth, maxHeight);
parseBorder1d(b2, bar2, maxWidth, maxHeight);
pending…
1c
parseBorder1c(b1, bar1, maxWidth, maxHeight);
parseBorder1c(b2, bar1, maxWidth, maxHeight);
parseBorder1c(b1, bar2, maxWidth, maxHeight);
parseBorder1c(b2, bar2, maxWidth, maxHeight);
pending…
1b
parseBorder1b(b1, bar1, maxWidth, maxHeight);
parseBorder1b(b2, bar1, maxWidth, maxHeight);
parseBorder1b(b1, bar2, maxWidth, maxHeight);
parseBorder1b(b2, bar2, maxWidth, maxHeight);
pending…

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

Compare results of other browsers

0 Comments