Reputation: 5090
I have a question regarding efficiency in JavaScript. I want to create a 'watcher' that watches a particular member and, upon change, executes a function. I have seen two methods:
1) A set interval that continuously compares the old value against the current value and fires the callback when there is a change
2) Object.watch
I have read that Object.watch creates some overhead, but I was curious as to which method above is the most efficient? If there are other methods, feel free to let me know!
Upvotes: 2
Views: 473
Reputation: 27460
(Side note: You should use Object.observe()
as Object.watch()
is FF specific.)
If this is about particular property then it is better in many senses to use virtual property mechanism and provide your own setter. Check Object.defineProperty
Upvotes: 0
Reputation: 1635
You could also wrap the member in an update function which fires an event after setting a new value, and listen for the event elsewhere in your code. That would be pretty efficient.
Upvotes: 2