David Bruant (2013-08-04T21:40:40.000Z)
Le 04/08/2013 00:10, Brandon Benvie a écrit :
> On 8/3/2013 12:30 PM, David Bruant wrote:
>> That said, I recently worked on a project and I reviewed a pull 
>> request with "typeof x === 'object'" to ask to replace to 'Object(x) 
>> === x'.
> On a side note, I think your version of isObject is problematic 
> because it requires allocating an object every time the function 
> encounters a primitive.
It's not required. That's the raw reading of the spec and that's 
probably what the implementations do, but it's not required. When 
reading "if(Object(x) === x)", the JS engine, at the time of calling 
"Object(x)" knows that the outcome won't be used except for its 
identity. It also knows that what matters isn't even the identity, but 
whether the identity is equal to x. In essence, the allocation isn't 
required per se.

> I'd prefer avoiding the unnecessary allocation in a function that is 
> likely to be called very often.
It's a very short-lived object anyway and Generational Garbage 
Collection is very efficient with them.

David
domenic at domenicdenicola.com (2013-08-12T05:23:57.770Z)
Le 04/08/2013 00:10, Brandon Benvie a écrit :
> On 8/3/2013 12:30 PM, David Bruant wrote:
>> That said, I recently worked on a project and I reviewed a pull 
>> request with "typeof x === 'object'" to ask to replace to 'Object(x) === x'.
>
> On a side note, I think your version of isObject is problematic 
> because it requires allocating an object every time the function 
> encounters a primitive.

It's not required. That's the raw reading of the spec and that's 
probably what the implementations do, but it's not required. When 
reading "if(Object(x) === x)", the JS engine, at the time of calling 
"Object(x)" knows that the outcome won't be used except for its 
identity. It also knows that what matters isn't even the identity, but 
whether the identity is equal to x. In essence, the allocation isn't 
required per se.

> I'd prefer avoiding the unnecessary allocation in a function that is 
> likely to be called very often.

It's a very short-lived object anyway and Generational Garbage 
Collection is very efficient with them.