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

Commit

Permalink
perf: use faster check for $$ prefix
Browse files Browse the repository at this point in the history
Use two calls to charAt instead of substr to detect a $$prefix in the shallowCopy functions.
This makes shallowCopy 25-50% faster (depending on which browser is used).
http://jsperf.com/angular-shallow-copy

Closes #5457
  • Loading branch information
kseamon authored and IgorMinar committed Dec 17, 2013
1 parent 5c97731 commit cb29632
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/Angular.js
Original file line number Diff line number Diff line change
Expand Up @@ -769,7 +769,7 @@ function shallowCopy(src, dst) {
for(var key in src) {
// shallowCopy is only ever called by $compile nodeLinkFn, which has control over src
// so we don't need to worry about using our custom hasOwnProperty here
if (src.hasOwnProperty(key) && key.substr(0, 2) !== '$$') {
if (src.hasOwnProperty(key) && key.charAt(0) !== '$' && key.charAt(1) !== '$') {
dst[key] = src[key];
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/ngResource/resource.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ function shallowClearAndCopy(src, dst) {
});

for (var key in src) {
if (src.hasOwnProperty(key) && key.substr(0, 2) !== '$$') {
if (src.hasOwnProperty(key) && key.charAt(0) !== '$' && key.charAt(1) !== '$') {
dst[key] = src[key];
}
}
Expand Down

4 comments on commit cb29632

@tbertenshaw
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Incidentally (whilst ie8 is still supported :) ) this test performs an average of 8% slower (the new method) on ie8 http://jsperf.com/angular-shallow-copy

@atomrc
Copy link
Contributor

@atomrc atomrc commented on cb29632 Jan 7, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The old and the new conditions are not equivalent.
Now the properties beginning with a single '$' are also removed from the resource.

@tbertenshaw
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just ran the latest version 6 times against ie8 and the "fasterone" was 8% slower 5 out of the 6 times i ran it
http://jsperf.com/angular-shallow-copy/4

@cesutherland
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated jsperf for changes in d2e4e49: http://jsperf.com/angular-shallow-copy/5

Please sign in to comment.