Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit bf6a79c

Browse files
jbedardpetebacondarwin
authored andcommitted
perf(*): use Object.create instead of creating temporary constructors
Closes #10058
1 parent 8ee8ffe commit bf6a79c

File tree

4 files changed

+8
-11
lines changed

4 files changed

+8
-11
lines changed

src/Angular.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ function int(str) {
362362

363363

364364
function inherit(parent, extra) {
365-
return extend(new (extend(function() {}, {prototype:parent}))(), extra);
365+
return extend(Object.create(parent), extra);
366366
}
367367

368368
/**

src/auto/injector.js

+3-6
Original file line numberDiff line numberDiff line change
@@ -822,14 +822,11 @@ function createInjector(modulesToLoad, strictDi) {
822822
}
823823

824824
function instantiate(Type, locals, serviceName) {
825-
var Constructor = function() {},
826-
instance, returnedValue;
827-
828825
// Check if Type is annotated and use just the given function at n-1 as parameter
829826
// e.g. someModule.factory('greeter', ['$window', function(renamed$window) {}]);
830-
Constructor.prototype = (isArray(Type) ? Type[Type.length - 1] : Type).prototype;
831-
instance = new Constructor();
832-
returnedValue = invoke(Type, instance, locals, serviceName);
827+
// Object creation: http://jsperf.com/create-constructor/2
828+
var instance = Object.create((isArray(Type) ? Type[Type.length - 1] : Type).prototype);
829+
var returnedValue = invoke(Type, instance, locals, serviceName);
833830

834831
return isObject(returnedValue) || isFunction(returnedValue) ? returnedValue : instance;
835832
}

src/ng/controller.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,10 @@ function $ControllerProvider() {
108108
//
109109
// This feature is not intended for use by applications, and is thus not documented
110110
// publicly.
111-
var Constructor = function() {};
112-
Constructor.prototype = (isArray(expression) ?
111+
// Object creation: http://jsperf.com/create-constructor/2
112+
var controllerPrototype = (isArray(expression) ?
113113
expression[expression.length - 1] : expression).prototype;
114-
instance = new Constructor();
114+
instance = Object.create(controllerPrototype);
115115

116116
if (identifier) {
117117
addIdentifier(locals, identifier, instance, constructor || expression.name);

src/ngRoute/route.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ var ngRouteModule = angular.module('ngRoute', ['ng']).
3636
*/
3737
function $RouteProvider() {
3838
function inherit(parent, extra) {
39-
return angular.extend(new (angular.extend(function() {}, {prototype:parent}))(), extra);
39+
return angular.extend(Object.create(parent), extra);
4040
}
4141

4242
var routes = {};

0 commit comments

Comments
 (0)