|
| 1 | +var app = angular.module('compileBenchmark', []); |
| 2 | + |
| 3 | +var registerDirective; |
| 4 | +app.config(function($compileProvider) { |
| 5 | + if ($compileProvider.debugInfoEnabled) { |
| 6 | + $compileProvider.debugInfoEnabled(false); |
| 7 | + } |
| 8 | + |
| 9 | + registerDirective = function(name, config) { |
| 10 | + $compileProvider.directive(name, function() { return config; }); |
| 11 | + }; |
| 12 | +}); |
| 13 | + |
| 14 | +app.controller('DataController', function($element, $compile, $scope, $templateCache) { |
| 15 | + //The set of directives which have been created |
| 16 | + var directivesCreated = {}; |
| 17 | + |
| 18 | + //The current template being tested |
| 19 | + var template; |
| 20 | + |
| 21 | + $scope.oJSON = JSON.stringify($scope.o = {}); |
| 22 | + |
| 23 | + $scope.$watch("oJSON", function(oJSON) { |
| 24 | + angular.copy(JSON.parse(oJSON), $scope.o); |
| 25 | + }); |
| 26 | + |
| 27 | + $scope.$watchCollection("o", function(options) { |
| 28 | + var json = $scope.oJSON = JSON.stringify(options); |
| 29 | + var directiveName = "test" + json.toLowerCase().replace(/[^a-z0-9]+/g, ''); |
| 30 | + |
| 31 | + if (!directivesCreated[directiveName]) { |
| 32 | + var directiveDef = {}; |
| 33 | + |
| 34 | + //Simple options |
| 35 | + directiveDef.replace = options.replace || false; |
| 36 | + directiveDef.transclude = options.transclude || false; |
| 37 | + directiveDef.multiElement = options.multiElement || false; |
| 38 | + directiveDef.controller = options.controller ? function testController(){} : undefined; |
| 39 | + |
| 40 | + //Template |
| 41 | + if (options.templateType) { |
| 42 | + if (options.templateType === 'template') { |
| 43 | + directiveDef.template = '<div></div>'; |
| 44 | + } else if (options.templateType === 'templateUrl') { |
| 45 | + $templateCache.put(directiveDef.templateUrl = directiveName + 'tmpl', '<div></div>'); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + //Link method(s) |
| 50 | + var link; |
| 51 | + if (options.preLink) { |
| 52 | + link = {pre: function testPreLink(){}, post: options.postLink && function testPostLink(){}}; |
| 53 | + } else if (options.postLink) { |
| 54 | + link = function testLink(){}; |
| 55 | + } |
| 56 | + |
| 57 | + //Compile/link declaration |
| 58 | + if (options.compile) { |
| 59 | + directiveDef.compile = function testCompile() { return link; }; |
| 60 | + } else if (link) { |
| 61 | + directiveDef.link = link; |
| 62 | + } |
| 63 | + |
| 64 | + registerDirective(directiveName, directivesCreated[directiveName] = directiveDef); |
| 65 | + } |
| 66 | + |
| 67 | + //Single vs multiElement spanning |
| 68 | + var eCount = options.elementCount || 1; |
| 69 | + if (eCount <= 1) { |
| 70 | + template = angular.element(document.createElement(directiveName)); |
| 71 | + } else { |
| 72 | + template = angular.element(); |
| 73 | + template[template.length++] = angular.element('<div>').attr(directiveName+'-start', '')[0]; |
| 74 | + for (var i = 1; i < eCount - 1; i++) { |
| 75 | + template[template.length++] = document.createElement('span'); |
| 76 | + } |
| 77 | + template[template.length++] = angular.element('<div>').attr(directiveName+'-end', '')[0]; |
| 78 | + } |
| 79 | + |
| 80 | + //Transcluded elements have children |
| 81 | + if (options.transclude) { |
| 82 | + template.append(document.createElement('div')); |
| 83 | + } |
| 84 | + |
| 85 | + //Root element vs has-parent |
| 86 | + if (options.wrap) { |
| 87 | + template = angular.element(document.createElement('div')).append(template); |
| 88 | + } |
| 89 | + }); |
| 90 | + |
| 91 | + |
| 92 | + |
| 93 | + // TEST STEPS / STATE |
| 94 | + |
| 95 | + var RUN_COUNT = 5000; |
| 96 | + |
| 97 | + var linkFns = []; |
| 98 | + var elements = []; |
| 99 | + function pushElements(elms) { |
| 100 | + elements.push(elms); |
| 101 | + return elms; |
| 102 | + } |
| 103 | + |
| 104 | + benchmarkSteps.push({ |
| 105 | + name: 'compile', |
| 106 | + fn: function compile() { |
| 107 | + for (var i=0; i<RUN_COUNT; i++) { |
| 108 | + linkFns[i] = $compile( pushElements(template.clone()) ); |
| 109 | + } |
| 110 | + } |
| 111 | + }); |
| 112 | + |
| 113 | + benchmarkSteps.push({ |
| 114 | + name: 'link-clone', |
| 115 | + fn: function linkClone() { |
| 116 | + for (var i=0; i<RUN_COUNT; i++) { |
| 117 | + linkFns[i]($scope, pushElements); |
| 118 | + } |
| 119 | + } |
| 120 | + }); |
| 121 | + |
| 122 | + benchmarkSteps.push({ |
| 123 | + name: 'link', |
| 124 | + fn: function link() { |
| 125 | + for (var i=0; i<RUN_COUNT; i++) { |
| 126 | + linkFns[i]($scope); |
| 127 | + } |
| 128 | + } |
| 129 | + }); |
| 130 | + |
| 131 | + benchmarkSteps.push({ |
| 132 | + name: 'apply', |
| 133 | + fn: function linkApply() { |
| 134 | + $scope.$apply(); |
| 135 | + } |
| 136 | + }); |
| 137 | + |
| 138 | + benchmarkSteps.push({ |
| 139 | + name: 'destroy', |
| 140 | + fn: function destory() { |
| 141 | + while (elements.length) { |
| 142 | + elements.pop().remove(); |
| 143 | + } |
| 144 | + linkFns = []; |
| 145 | + } |
| 146 | + }); |
| 147 | +}); |
0 commit comments