Hey,
Do you have any official statement regarding these two functions?
Do you recommend usage of these functions to your developers?
We certainly recommend treating everything as immutable.
Object.freeze and Object.seal can't work in pre-ES5 engines, and drastically slow down every ES5+ engine - when it's really really important that something not be mutable, it might be worth using them.
We don't have an official recommendation either way.
@ljharb Are there measurements showing these slowdowns?
My recommendation would be to use them despite the slowdowns if you can stand it. If much code does this, then they will eventually no longer be slower. Indeed, they will become faster, since they present optimization opportunities absent from non-sealed and non-frozen objects.
The most important first step to getting these not to be slower is to incorporate them into some influential benchmarks.
i don't have any measurements offhand, but it's been measured in the past and is one of those "sadly true" common knowledge things, like "bind is slow" (although v8 finally made it not be slow, very recently, so this might change).
Incorporating them into benchmarks and speeding them up seems great; but until they are faster, I hesitate to recommend them here.
Aha! I was hoping that was the answer. They used to be slow on v8. Then v8 did a bunch of work to make them no slower than non-frozen objects. Unless there is modern data to the contrary, we should assume this "common knowledge" is a fossil from an earlier time.
I'd say that until data appears that disproves this "common knowledge" (which was indeed once true), it remains true.
If you can provide data that all four of the modern engines have optimized frozen objects, I will be very happy to update the advice here.
I tried https://jsperf.com/freeze-vs-seal-vs-normal/3 and https://jsperf.com/freeze-vs-seal-vs-normal/28 on Chrome, FF, and Safari. I don't know why the bar charts are not updating. I am traveling and do not currently have access to a machine running Edge.
Could someone who has access to all four browsers, and knows how to get the displayed data to update, try these on all four? Thanks.
I've run https://jsperf.com/freeze-vs-seal-vs-normal/28 on Edge, Safari, Chrome and FF and there are no performance differences (+- 3% on random cases).
So, to my eye performance is not an issue at all on Chrome, FF, Safari and Edge for freeze and seal. In this case would you recommend using them for everything that needs to be marked as immutable?
@finalclass provided you've shammed them with self-returning functions for pre-ES5 environments or conditionally call them, and absent any performance issues, absolutely!
I think this has been answered.
offtopic, but damn this is surprising. regular for-loops also iterate frozen objects 97x slower than non-frozen ones.