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

perf(*): use Object.create instead of creating temporary constructors #10058

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Angular.js
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ function int(str) {


function inherit(parent, extra) {
return extend(new (extend(function() {}, {prototype:parent}))(), extra);
return extend(Object.create(parent), extra);
}

/**
Expand Down
9 changes: 3 additions & 6 deletions src/auto/injector.js
Original file line number Diff line number Diff line change
Expand Up @@ -815,14 +815,11 @@ function createInjector(modulesToLoad, strictDi) {
}

function instantiate(Type, locals, serviceName) {
var Constructor = function() {},
instance, returnedValue;

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

return isObject(returnedValue) || isFunction(returnedValue) ? returnedValue : instance;
}
Expand Down
6 changes: 3 additions & 3 deletions src/ng/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,10 @@ function $ControllerProvider() {
//
// This feature is not intended for use by applications, and is thus not documented
// publicly.
var Constructor = function() {};
Constructor.prototype = (isArray(expression) ?
// Object creation: http://jsperf.com/create-constructor/2
var controllerPrototype = (isArray(expression) ?
expression[expression.length - 1] : expression).prototype;
instance = new Constructor();
instance = Object.create(controllerPrototype);

if (identifier) {
addIdentifier(locals, identifier, instance, constructor || expression.name);
Expand Down
2 changes: 1 addition & 1 deletion src/ngRoute/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ var ngRouteModule = angular.module('ngRoute', ['ng']).
*/
function $RouteProvider() {
function inherit(parent, extra) {
return angular.extend(new (angular.extend(function() {}, {prototype:parent}))(), extra);
return angular.extend(Object.create(parent), extra);
}

var routes = {};
Expand Down