Rectangle 27 171

The slow and reliable in operator and hasOwnProperty method

How do I check if a particular key exists in a JavaScript object or array? If a key doesn't exist and I try to access it, will it return false? Or throw an error?

Accessing directly a missing property using (associative) array style or object style will return an undefined constant.

As people have already mentioned here, you could have an object with a property associated with an "undefined" constant.

var bizzareObj = {valid_key:  undefined};

In that case, you will have to use hasOwnProperty or in operator to know if the key is really there. But, but at what price?

in operator and hasOwnProperty are "methods" that use Property Descriptor mechanism in Javascript (similar to Java reflection in the Java language).

The Property Descriptor type is used to explain the manipulation and reification of named property attributes. Values of the Property Descriptor type are records composed of named fields where each fields name is an attribute name and its value is a corresponding attribute value as specified in 8.6.1. In addition, any field may be present or absent.

On the other hand, calling an object method or key will use Javascript [[Get]] mechanism. That is far way faster!

var result = "Impression" in array;

The result was

12,931,832 0.21% ops/sec      92% slower
var result = array.hasOwnProperty("Impression")
16,021,758 0.45% ops/sec     91% slower
var result = array["Impression"] === undefined
168,270,439 0.13 ops/sec     0.02% slower
var result = array.Impression  === undefined;

The result was

168,303,172 0.20%     fastest
null
undefined

null is the primitive value that represents the intentional absence of any object value, or in short terms, the confirmed lack of value. On the other hand, undefined is unknown value (not defined). If there is a property that will be used later with a proper value consider use null reference instead of undefined because in the initial moment the property is confirmed to lack a value.

var a = {1: null}; 
console.log(a[1] === undefined); // output: false. I know the value at position 1 of a[] is absent and this was by design, i.e.:  the value is defined. 
console.log(a[0] === undefined); // output: true. I cannot say anything about a[0] value. In this case, the key 0 was not in a[].

Avoid objects with undefined values. Check directly whenever possible and use null to initialize property values. Otherwise, use the slow in operator or hasOwnProperty() method.

Are all of these methods acceptable in all commonly used browsers, e.g. IE8+?

+1 for benchmarking. Thank you, this is exactly the information I was hoping to find. Definitely a strong argument to write code that never assigns or expects a key to contain the value undefined.

I was curious how Underscore.js's has() compared, so I added it to the jsperf (version 11). Turns out it's in the slow group along with in and hasOwnProperty().

One reason I would set undefined to a hash value is that I actually wanted to delete that property key from the hash, but delete hash[key] is much slower than hash[key] = undefined. Of course in this case it makes no sense for me to need the in operator, but it acts as a counterexample to we should always avoid setting value to undefined.

arrays - Checking if a key exists in a JavaScript object? - Stack Over...

javascript arrays object
Rectangle 27 170

The slow and reliable in operator and hasOwnProperty method

How do I check if a particular key exists in a JavaScript object or array? If a key doesn't exist and I try to access it, will it return false? Or throw an error?

Accessing directly a missing property using (associative) array style or object style will return an undefined constant.

As people have already mentioned here, you could have an object with a property associated with an "undefined" constant.

var bizzareObj = {valid_key:  undefined};

In that case, you will have to use hasOwnProperty or in operator to know if the key is really there. But, but at what price?

in operator and hasOwnProperty are "methods" that use Property Descriptor mechanism in Javascript (similar to Java reflection in the Java language).

The Property Descriptor type is used to explain the manipulation and reification of named property attributes. Values of the Property Descriptor type are records composed of named fields where each fields name is an attribute name and its value is a corresponding attribute value as specified in 8.6.1. In addition, any field may be present or absent.

On the other hand, calling an object method or key will use Javascript [[Get]] mechanism. That is far way faster!

var result = "Impression" in array;

The result was

12,931,832 0.21% ops/sec      92% slower
var result = array.hasOwnProperty("Impression")
16,021,758 0.45% ops/sec     91% slower
var result = array["Impression"] === undefined
168,270,439 0.13 ops/sec     0.02% slower
var result = array.Impression  === undefined;

The result was

168,303,172 0.20%     fastest
null
undefined

null is the primitive value that represents the intentional absence of any object value, or in short terms, the confirmed lack of value. On the other hand, undefined is unknown value (not defined). If there is a property that will be used later with a proper value consider use null reference instead of undefined because in the initial moment the property is confirmed to lack a value.

var a = {1: null}; 
console.log(a[1] === undefined); // output: false. I know the value at position 1 of a[] is absent and this was by design, i.e.:  the value is defined. 
console.log(a[0] === undefined); // output: true. I cannot say anything about a[0] value. In this case, the key 0 was not in a[].

Avoid objects with undefined values. Check directly whenever possible and use null to initialize property values. Otherwise, use the slow in operator or hasOwnProperty() method.

Are all of these methods acceptable in all commonly used browsers, e.g. IE8+?

+1 for benchmarking. Thank you, this is exactly the information I was hoping to find. Definitely a strong argument to write code that never assigns or expects a key to contain the value undefined.

I was curious how Underscore.js's has() compared, so I added it to the jsperf (version 11). Turns out it's in the slow group along with in and hasOwnProperty().

One reason I would set undefined to a hash value is that I actually wanted to delete that property key from the hash, but delete hash[key] is much slower than hash[key] = undefined. Of course in this case it makes no sense for me to need the in operator, but it acts as a counterexample to we should always avoid setting value to undefined.

arrays - Checking if a key exists in a JavaScript object? - Stack Over...

javascript arrays object
Rectangle 27 170

The slow and reliable in operator and hasOwnProperty method

How do I check if a particular key exists in a JavaScript object or array? If a key doesn't exist and I try to access it, will it return false? Or throw an error?

Accessing directly a missing property using (associative) array style or object style will return an undefined constant.

As people have already mentioned here, you could have an object with a property associated with an "undefined" constant.

var bizzareObj = {valid_key:  undefined};

In that case, you will have to use hasOwnProperty or in operator to know if the key is really there. But, but at what price?

in operator and hasOwnProperty are "methods" that use Property Descriptor mechanism in Javascript (similar to Java reflection in the Java language).

The Property Descriptor type is used to explain the manipulation and reification of named property attributes. Values of the Property Descriptor type are records composed of named fields where each fields name is an attribute name and its value is a corresponding attribute value as specified in 8.6.1. In addition, any field may be present or absent.

On the other hand, calling an object method or key will use Javascript [[Get]] mechanism. That is far way faster!

var result = "Impression" in array;

The result was

12,931,832 0.21% ops/sec      92% slower
var result = array.hasOwnProperty("Impression")
16,021,758 0.45% ops/sec     91% slower
var result = array["Impression"] === undefined
168,270,439 0.13 ops/sec     0.02% slower
var result = array.Impression  === undefined;

The result was

168,303,172 0.20%     fastest
null
undefined

null is the primitive value that represents the intentional absence of any object value, or in short terms, the confirmed lack of value. On the other hand, undefined is unknown value (not defined). If there is a property that will be used later with a proper value consider use null reference instead of undefined because in the initial moment the property is confirmed to lack a value.

var a = {1: null}; 
console.log(a[1] === undefined); // output: false. I know the value at position 1 of a[] is absent and this was by design, i.e.:  the value is defined. 
console.log(a[0] === undefined); // output: true. I cannot say anything about a[0] value. In this case, the key 0 was not in a[].

Avoid objects with undefined values. Check directly whenever possible and use null to initialize property values. Otherwise, use the slow in operator or hasOwnProperty() method.

Are all of these methods acceptable in all commonly used browsers, e.g. IE8+?

+1 for benchmarking. Thank you, this is exactly the information I was hoping to find. Definitely a strong argument to write code that never assigns or expects a key to contain the value undefined.

I was curious how Underscore.js's has() compared, so I added it to the jsperf (version 11). Turns out it's in the slow group along with in and hasOwnProperty().

One reason I would set undefined to a hash value is that I actually wanted to delete that property key from the hash, but delete hash[key] is much slower than hash[key] = undefined. Of course in this case it makes no sense for me to need the in operator, but it acts as a counterexample to we should always avoid setting value to undefined.

arrays - Checking if a key exists in a JavaScript object? - Stack Over...

javascript arrays object
Rectangle 27 170

The slow and reliable in operator and hasOwnProperty method

How do I check if a particular key exists in a JavaScript object or array? If a key doesn't exist and I try to access it, will it return false? Or throw an error?

Accessing directly a missing property using (associative) array style or object style will return an undefined constant.

As people have already mentioned here, you could have an object with a property associated with an "undefined" constant.

var bizzareObj = {valid_key:  undefined};

In that case, you will have to use hasOwnProperty or in operator to know if the key is really there. But, but at what price?

in operator and hasOwnProperty are "methods" that use Property Descriptor mechanism in Javascript (similar to Java reflection in the Java language).

The Property Descriptor type is used to explain the manipulation and reification of named property attributes. Values of the Property Descriptor type are records composed of named fields where each fields name is an attribute name and its value is a corresponding attribute value as specified in 8.6.1. In addition, any field may be present or absent.

On the other hand, calling an object method or key will use Javascript [[Get]] mechanism. That is far way faster!

var result = "Impression" in array;

The result was

12,931,832 0.21% ops/sec      92% slower
var result = array.hasOwnProperty("Impression")
16,021,758 0.45% ops/sec     91% slower
var result = array["Impression"] === undefined
168,270,439 0.13 ops/sec     0.02% slower
var result = array.Impression  === undefined;

The result was

168,303,172 0.20%     fastest
null
undefined

null is the primitive value that represents the intentional absence of any object value, or in short terms, the confirmed lack of value. On the other hand, undefined is unknown value (not defined). If there is a property that will be used later with a proper value consider use null reference instead of undefined because in the initial moment the property is confirmed to lack a value.

var a = {1: null}; 
console.log(a[1] === undefined); // output: false. I know the value at position 1 of a[] is absent and this was by design, i.e.:  the value is defined. 
console.log(a[0] === undefined); // output: true. I cannot say anything about a[0] value. In this case, the key 0 was not in a[].

Avoid objects with undefined values. Check directly whenever possible and use null to initialize property values. Otherwise, use the slow in operator or hasOwnProperty() method.

Are all of these methods acceptable in all commonly used browsers, e.g. IE8+?

+1 for benchmarking. Thank you, this is exactly the information I was hoping to find. Definitely a strong argument to write code that never assigns or expects a key to contain the value undefined.

I was curious how Underscore.js's has() compared, so I added it to the jsperf (version 11). Turns out it's in the slow group along with in and hasOwnProperty().

One reason I would set undefined to a hash value is that I actually wanted to delete that property key from the hash, but delete hash[key] is much slower than hash[key] = undefined. Of course in this case it makes no sense for me to need the in operator, but it acts as a counterexample to we should always avoid setting value to undefined.

arrays - Checking if a key exists in a JavaScript object? - Stack Over...

javascript arrays object
Rectangle 27 170

The slow and reliable in operator and hasOwnProperty method

How do I check if a particular key exists in a JavaScript object or array? If a key doesn't exist and I try to access it, will it return false? Or throw an error?

Accessing directly a missing property using (associative) array style or object style will return an undefined constant.

As people have already mentioned here, you could have an object with a property associated with an "undefined" constant.

var bizzareObj = {valid_key:  undefined};

In that case, you will have to use hasOwnProperty or in operator to know if the key is really there. But, but at what price?

in operator and hasOwnProperty are "methods" that use Property Descriptor mechanism in Javascript (similar to Java reflection in the Java language).

The Property Descriptor type is used to explain the manipulation and reification of named property attributes. Values of the Property Descriptor type are records composed of named fields where each fields name is an attribute name and its value is a corresponding attribute value as specified in 8.6.1. In addition, any field may be present or absent.

On the other hand, calling an object method or key will use Javascript [[Get]] mechanism. That is far way faster!

var result = "Impression" in array;

The result was

12,931,832 0.21% ops/sec      92% slower
var result = array.hasOwnProperty("Impression")
16,021,758 0.45% ops/sec     91% slower
var result = array["Impression"] === undefined
168,270,439 0.13 ops/sec     0.02% slower
var result = array.Impression  === undefined;

The result was

168,303,172 0.20%     fastest
null
undefined

null is the primitive value that represents the intentional absence of any object value, or in short terms, the confirmed lack of value. On the other hand, undefined is unknown value (not defined). If there is a property that will be used later with a proper value consider use null reference instead of undefined because in the initial moment the property is confirmed to lack a value.

var a = {1: null}; 
console.log(a[1] === undefined); // output: false. I know the value at position 1 of a[] is absent and this was by design, i.e.:  the value is defined. 
console.log(a[0] === undefined); // output: true. I cannot say anything about a[0] value. In this case, the key 0 was not in a[].

Avoid objects with undefined values. Check directly whenever possible and use null to initialize property values. Otherwise, use the slow in operator or hasOwnProperty() method.

Are all of these methods acceptable in all commonly used browsers, e.g. IE8+?

+1 for benchmarking. Thank you, this is exactly the information I was hoping to find. Definitely a strong argument to write code that never assigns or expects a key to contain the value undefined.

I was curious how Underscore.js's has() compared, so I added it to the jsperf (version 11). Turns out it's in the slow group along with in and hasOwnProperty().

One reason I would set undefined to a hash value is that I actually wanted to delete that property key from the hash, but delete hash[key] is much slower than hash[key] = undefined. Of course in this case it makes no sense for me to need the in operator, but it acts as a counterexample to we should always avoid setting value to undefined.

arrays - Checking if a key exists in a JavaScript object? - Stack Over...

javascript arrays object
Rectangle 27 0

The slow and reliable in operator and hasOwnProperty method

How do I check if a particular key exists in a JavaScript object or array? If a key doesn't exist and I try to access it, will it return false? Or throw an error?

Accessing directly a missing property using (associative) array style or object style will return an undefined constant.

As people have already mentioned here, you could have an object with a property associated with an "undefined" constant.

var bizzareObj = {valid_key:  undefined};

In that case, you will have to use hasOwnProperty or in operator to know if the key is really there. But, but at what price?

in operator and hasOwnProperty are "methods" that use Property Descriptor mechanism in Javascript (similar to Java reflection in the Java language).

The Property Descriptor type is used to explain the manipulation and reification of named property attributes. Values of the Property Descriptor type are records composed of named fields where each fields name is an attribute name and its value is a corresponding attribute value as specified in 8.6.1. In addition, any field may be present or absent.

On the other hand, calling an object method or key will use Javascript [[Get]] mechanism. That is far way faster!

var result = "Impression" in array;

The result was

12,931,832 0.21% ops/sec      92% slower
var result = array.hasOwnProperty("Impression")
16,021,758 0.45% ops/sec     91% slower
var result = array["Impression"] === undefined
168,270,439 0.13 ops/sec     0.02% slower
var result = array.Impression  === undefined;

The result was

168,303,172 0.20%     fastest

Avoid objects with undefined values. Check directly whenever possible. Otherwise, use in operator or hasOwnProperty method.

Are all of these methods acceptable in all commonly used browsers, e.g. IE8+?

+1 for benchmarking. Thank you, this is exactly the information I was hoping to find. Definitely a strong argument to write code that never assigns or expects a key to contain the value undefined.

I was curious how Underscore.js's has() compared, so I added it to the jsperf (version 11). Turns out it's in the slow group along with in and hasOwnProperty().

Checking if an array key exists in a JavaScript object or array? - Sta...

javascript arrays object
Rectangle 27 0

in

var o = { 'foo': 'bar' };

console.log('constructor' in o); // TRUE
console.log('foo' in o); // TRUE

console.log(o.hasOwnProperty('constructor')); // FALSE
console.log(o.hasOwnProperty('foo')); // TRUE

Every object descended from Object inherits the hasOwnProperty method. This method can be used to determine whether an object has the specified property as a direct property of that object; unlike the in operator, this method does not check down the object's prototype chain.

The in operator returns true if the specified property is in the specified object.

Cool, but you need to explain your answer, not just post some code.

javascript - what's the difference with hasOwnProperty and in? - Stack...

javascript hasownproperty