jsPerf.app is an online JavaScript performance benchmark test runner & jsperf.com mirror. It is a complete rewrite in homage to the once excellent jsperf.com now with hopefully a more modern & maintainable codebase.
jsperf.com URLs are mirrored at the same path, e.g:
https://jsperf.com/negative-modulo/2
Can be accessed at:
https://jsperf.app/negative-modulo/2
This test compares performance of $watching all elements of the array before and perform a calculation when the targeted values change. The other approach simply use a filter that will always cause the calculations to happen again.
<script src="//cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.3/angular.min.js">
</script>
<script type="text/javascript">
// angular bootstraping
var elementFiltered = angular.element('<div ng-controller="FilteredCtrl">{{values | summit}}</div>');
var elementWatched = angular.element('<div ng-controller="WatchedCtrl">{{result}}</span></div>');
angular.module('test', []).filter('summit', function() {
return function(values) {
var result = 0;
for (var i = 0; i < values.length; i++) {
result += values[i].value;
}
return result;
}
});
function FilteredCtrl() {
// nothing to do
}
function WatchedCtrl($scope) {
$scope.$watch('values', function(values) {
$scope.result = 0;
for (var i = 0; i < values.length; i++) {
$scope.result += values[i].value;
}
}, true);
}
angular.bootstrap(elementFiltered, ['test']);
angular.bootstrap(elementWatched, []);
var scopeFiltered = elementFiltered.scope(),
scopeWatched = elementWatched.scope();
</script>
// test related
var i, current, expected;
function randomize(values) {
var current, i;
expected = 0;
for (i = 0; i < 100; i++) {
current = values[i] || (values[i] = {});
current.value = Math.round(Math.random() * 250);
expected += current.value;
}
}
scopeFiltered.values = [];
scopeWatched.values = [];
randomize(scopeFiltered.values);
randomize(scopeWatched.values);
scopeFiltered.$apply();
scopeWatched.$apply();
delete scopeFiltered.values;
delete scopeWatched.values;
expected = null;
Ready to run.
Test | Ops/sec | |
---|---|---|
filter - values changed |
| ready |
watch - values changed |
| ready |
filter - values not changed |
| ready |
watch - values not changed |
| ready |
You can edit these tests or add more tests to this page by appending /edit to the URL.