-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Perform hasOwnProperty validation #446
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm +1 on adding guard-for-in to our lint config.
|
I do worry about performance with this though — it looks expensive to add a function call to every loop, especially for some of these which are called on each tick or more. Perhaps we should just keep an eye on it, and in the most expensive cases, cache and iterate with |
|
I had been thinking just the same thing.
Is there a legitimate reason to worry about the object model being
compremised in this way? I have been actively removing them where possible
when there was no chance of it causing issue in the normal running of
scratch 3 on my performance branch.
On 10 Feb 2017 6:14 p.m., "Ray Schamp" <[email protected]> wrote:
I do worry about performance with this though — it looks expensive to add a
function call to every loop, especially for some of these which are called
on each tick or more. Perhaps we should just keep an eye on it, and in the
most expensive cases, cache and iterate with Object.keys(objToIterate) or
something?
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#446 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AGbNvjy2TTSHfAfV3-Da0er4Pn7JNCiLks5rbKkIgaJpZM4L86zR>
.
|
|
Have been enjoying watching these discussions :) I got ~90% slower with the guard: https://jsperf.com/for-in-hasownproperty-or-not - although I do think it's probably an extreme case, not sure if any loop in the execution path is executing enough times per tick to make it significant. |
|
My favorite example to explain what Object.prototype.harmless = falseFor example: var stuff = {a: 1, b: 2}
for (var propertyName in stuff) { console.log(propertyName, '=', stuff[propertyName]) }The output of the above: Even with that demo, though, there are two valid questions being discussed here: Is this really something to worry about?Extending What I really worry about is libraries which extend, say, More info about this can be found here: http://bonsaiden.github.io/JavaScript-Garden/#object.forinloop Won't this hurt performance?I agree with @rschamp when he says:
Also, in my opinion, @tmickel's benchmark above actually indicates that this isn't very expensive. That benchmark tests the cost of adding one line of code to a block of JavaScript: var count = 0;
for (var test in x) {
if (!x.hasOwnProperty(test)) continue; // <---- how expensive is this line?
count++;
}The result: for Tim it runs about 90% slower, and on my system it's about 80% slower. Let's go with the 90% number since it's more dire. When jsPerf says one piece of code is "90% slower" than another, it really means that Let's algebra!
So, roughly speaking, the line var p;
for (var b = 0; b < 9; ++b) {}While that's not the same as "free" it seems fairly cheap to me -- especially considering that most of these pieces of code don't run with high frequency. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
tested locally
Resolves
#366
Proposed Changes
add hasOwnProperty check to for loops in vm
Reason for Changes
without it things like
Object.prototype.harmless = false;break the appThat still breaks it because there is the same problem in the audio engine
Test Coverage
I didn't add it but there is this: http://eslint.org/docs/2.0.0/rules/guard-for-in