socket.io-stream icon indicating copy to clipboard operation
socket.io-stream copied to clipboard

Warning : mutating the [[Prototype]] of an object

Open Yeh-Jack opened this issue 9 years ago • 2 comments

The code base has 3 statements around the allocate(that, length) function use the '.proto =' assignment which makes FireFox 45.0 (openSUSE 42.1) claims :

mutating the [[Prototype]] of an object will cause your code to run very slowly; instead create the object with the correct initial [[Prototype]] value using Object.create.

Please refer to the page : http://stackoverflow.com/questions/23807805/why-is-mutating-the-prototype-of-an-object-bad-for-performance for more explanation.

Based on the performance test page : http://jsperf.com/proto-vs-object-create2, by the use of the '.proto =' assignment is 32% slower than Object.create().

I change the code personally as : ` if (Buffer.TYPED_ARRAY_SUPPORT) { // Buffer.prototype.proto = Uint8Array.prototype // Buffer.proto = Uint8Array Buffer.prototype = Object.create(Uint8Array.prototype) Buffer = Object.create(Uint8Array) }

function allocate (that, length) { if (Buffer.TYPED_ARRAY_SUPPORT) { // Return an augmented Uint8Array instance, for best performance that = Buffer._augment(new Uint8Array(length)) // that.proto = Buffer.prototype that = Object.create(Buffer.prototype) } else { // Fallback: Return an object instance of the Buffer class that.length = length that._isBuffer = true }

var fromPool = length !== 0 && length <= Buffer.poolSize >>> 1 if (fromPool) that.parent = rootParent

return that } `

Please verify the changes are appropriate or not.

Thank you all.

Yeh-Jack avatar Mar 24 '16 04:03 Yeh-Jack

I've applied these changes and the module stopped working.

JorgeFPastor avatar Jun 18 '16 17:06 JorgeFPastor

Sorry, I use XHR for streaming now, but as I recall it works for me in the day that I post this issue and use the solution for a while. Since I took another solution for my project, this package is removed already thus I can't re-produce this again.

Sorry ...

Yeh-Jack avatar Jun 20 '16 01:06 Yeh-Jack