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

fix(ng): in angular.copy do not call hasOwnProperty on objects with not ... #10116

Closed
wants to merge 1 commit into from

Conversation

mohsen1
Copy link
Contributor

@mohsen1 mohsen1 commented Nov 18, 2014

...prototype

angular.copy can be called with an object that has no prototype. Call
Object.prototype.hasOwnProperty instead of object.hasOwnProperty to
safely check if object has a property

… prototype

angular.copy can be called with an object that has no prototype. Call
Object.prototype.hasOwnProperty instead of object.hasOwnProperty to
safely check if object has a property
@lgalfaso
Copy link
Contributor

Would it be possible to get some numbers to know if this will have some
performance implications?

@mohsen1
Copy link
Contributor Author

mohsen1 commented Nov 19, 2014

@gkalpak
Copy link
Member

gkalpak commented Nov 19, 2014

Calling it on the same item might allow browser optimizations that hide the actual performance.

In practice, it will be called on many different objects (and will correspond to a small percentage of the overall processing time, so I don't expect it to have any significant impact.

Tried with 1,000,000 different objects and the results are equal: http://jsperf.com/instance-vs-prototype-hasownproperty
At 100,000 the instance method was faster (by a varying percentage of 10 - 20).

@gkalpak
Copy link
Member

gkalpak commented Nov 19, 2014

Here is another jsperf featuring 2 alternative versions of copy() (along with the original one):

  1. Using hasOwnProperty.call().
  2. Using Object.keys() (since IE8 is no longer an issue).

http://jsperf.com/angularjs-copy-with-hasownproperty-call

@lgalfaso
Copy link
Contributor

I would not be happy to have a 10% performance hit...

@petebacondarwin @caitp WDYT?

@lgalfaso lgalfaso modified the milestones: 1.3.x, Backlog Nov 19, 2014
@mohsen1
Copy link
Contributor Author

mohsen1 commented Nov 19, 2014

There isn't a performance difference really. @gkalpak tests are not using JSPerf right. JSPerf does iterations itself. If you put a huge loop in your snippet the result might become less accurate.

I've added .apply for comparison to the JSPerf. They are all the same
http://jsperf.com/hasownproperty-context/2

@googlebot
Copy link

Thanks for your pull request.

It looks like this may be your first contribution to a Google open source project, in which case you'll need to sign a Contributor License Agreement (CLA) at https://cla.developers.google.com/.

If you've already signed a CLA, it's possible we don't have your GitHub username or you're using a different email address. Check the information on your CLA or see this help article on setting the email on your git commits.

Once you've done that, please reply here to let us know. If you signed the CLA as a corporation, please let us know the company's name.

@gkalpak
Copy link
Member

gkalpak commented Nov 19, 2014

I don't have a loop in my last jsperf: http://jsperf.com/angularjs-copy-with-hasownproperty-call

I implemented the whole copy() function to get more "realistic" results.
(But I am not a jsperf guy, so there is a good chance I might be doing something wrong).

@mary-poppins
Copy link

CLA signature verified! Thank you!

Someone from the team will now triage your PR and it will be processed based on the determined priority (doc updates and fixes with tests are prioritized over other changes).

3 similar comments
@mary-poppins
Copy link

CLA signature verified! Thank you!

Someone from the team will now triage your PR and it will be processed based on the determined priority (doc updates and fixes with tests are prioritized over other changes).

@mary-poppins
Copy link

CLA signature verified! Thank you!

Someone from the team will now triage your PR and it will be processed based on the determined priority (doc updates and fixes with tests are prioritized over other changes).

@mary-poppins
Copy link

CLA signature verified! Thank you!

Someone from the team will now triage your PR and it will be processed based on the determined priority (doc updates and fixes with tests are prioritized over other changes).

@mary-poppins
Copy link

CLA signature verified! Thank you!

Someone from the team will now triage your PR and it will be processed based on the determined priority (doc updates and fixes with tests are prioritized over other changes).

@lgalfaso
Copy link
Contributor

lgalfaso commented Jun 8, 2015

This PR is no longer valid, this is already fixed at master

@lgalfaso lgalfaso closed this Jun 8, 2015
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants