HasOwnProperty () does not work in Chrome for array

I have inherited some Javascript code and I am not a Javascript expert.

We have an object that acts as a collection of hashes and values ​​called buckets . It has properties that are a hash value, and each property is an object. Here's what it looks like in a browser debugger:

enter image description here

We have a containsKey() function that uses hasOwnProperty() to check for the hash in the buckets object.

 containsKey: function(key) { var hash = this.comparer.getObjectHashCode(key); if (!this.buckets.hasOwnProperty(hash)) return false; var array = this.buckets[hash]; for (var i = 0; i < array.length; i++) { if (this.comparer.areEqual(array[i].key, key)) return true; } return false; } 

This code has worked flawlessly for at least 3 years. In the last week or two, it has stopped working in Chrome. Still works fine in IE (not sure about FF).

It seems to me that he should continue to work. I confirmed that buckets contains the hash property buckets are looking for. But hasOwnProperty() now returns false.

Is there a more suitable function that I should use here?

Here, where it does not work in the debugger:

enter image description here

+6
source share
1 answer

There was some kind of error in Chrome that was introduced when we upgraded the Chrome version from 54.0.2840.99 to 55.0.2883.75 on December 2, 2016.

The solution to our specific problem was to modify our hash function to return only positive numbers. Although small tests using negative numbers seem to work fine (according to the strabismus example in the comments), in our application they no longer work in Chrome.

I don’t have much time to delve into it. I do not know if this is due to the number of elements (we only have about 170 items in our "bucket").

Update:

gre_gor, a sample was created in the above comment showing an error:

 obj = { buckets: {}, comparer: { getObjectHashCode: function(str) { // hardcoded magic hashing return { "SUPPLYINVENTORY/SUPTRANSENTRY": -1525029354, "PROPANE/LOADPROPANETOGROWERAR": 115289505 }[str.toUpperCase()]; }, areEqual: function(a, b) { return a.toUpperCase() == b.toUpperCase(); } }, containsKey: function(key) { var hash = this.comparer.getObjectHashCode(key); if (!this.buckets.hasOwnProperty(hash)) return false; var array = this.buckets[hash]; for (var i = 0; i < array.length; i++) { if (this.comparer.areEqual(array[i].key, key)) return true; } return false; } }; obj.buckets[-1525029354] = [{ key: "SUPPLYINVENTORY/SUPTRANSENTRY", value: "$SupTransEntry object" }]; obj.buckets[115289505] = [{ key: "PROPANE/LOADPROPANETOGROWERAR", value: "$LoadPropaneToGrowerAR object" }]; console.log(obj.containsKey("SUPPLYINVENTORY/SUPTRANSENTRY"), obj.containsKey("PROPANE/LOADPROPANETOGROWERAR")); 

The text "true true" should go to the console, but in Chrome 55 it generates "false true".

Thanks to gre_gor for a test that reliably reproduces the problem. I reported a Google bug.

Update # 2: An error was sent 3 days before my submission. The problem has been fixed and will be released soon, and I won’t have to bypass it. - Chromium Bug # 673008

+6
source

Source: https://habr.com/ru/post/1013096/


All Articles