-
-
Notifications
You must be signed in to change notification settings - Fork 8.6k
fix(reactivity): process subtypes of collections properly #1799
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
Same as #1490 |
That implementation introduces unnecessary impact of the runtime performance. But time loss caused by my implementation can be acceptable I have run a simple test (node v12.18.0 on win10 using
|
Can you please post the code for the performance test, I am just curious :) |
To prevent the optimization from engine affecting the test, I ran tests manually in sections (so I call it simple test). But for show, I write this simple test, and the result is that my running time is less than half of that const collectionTypes = new Set([Set, Map, WeakMap, WeakSet])
class CustomMap extends Map {
}
class CustomSet extends Set {
}
class CustomWeakMap extends WeakMap {
}
class CustomWeakSet extends WeakSet {
}
const testCases = new Set()
for (let i = 0; i < 1000; ++i) {
switch (Math.floor(Math.random() * 5)) {
case 0:
testCases.add(new CustomMap())
break
case 1:
testCases.add(new CustomSet())
break
case 2:
testCases.add(new CustomWeakMap())
break
case 3:
testCases.add(new CustomWeakSet())
break
default:
testCases.add({})
}
}
console.time('a')
testCases.forEach(testCase => {
[...collectionTypes].some(c => testCase instanceof c)
})
console.timeEnd('a')
let hasB = false
console.time('b')
testCases.forEach(testCase => {
for (const collectionType of collectionTypes) {
if (testCase instanceof collectionType) {
hasB = true
break
}
}
})
console.timeEnd('b')
console.time('c')
testCases.forEach(testCase => {
})
console.timeEnd('c') |
I changed the number of for loops to
It seems that the performance gap between the two implementations is not big. |
Optimization works well, but I don't think such scenario is realistic. or is it possible to use a method of good performance but dirty that marking collection types in prototypes? I've just pushed. |
Yep. This is a implementation with good performance but will pollute the prototype. I am not sure this is a good solution. 🧐 |
I think it's a trade-off: either restrict usage, or lose some performance, or fail to keep things neat. There needs to be a decision to solve the problem. After all, there is no guarantee that no subtypes of collection used in a project or imported third-party libraries. |
Fixed usage scenarios using subtypes of collections: