Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does JavaScript VM implements Object property access? Is it Hashtable?

Objects in JavaScript can be used as Hashtable (the key must be String) Is it perform well as Hashtable the data structure?

I mean , does it implemented as Hashtable behind the scene?

Update: (1) I changed HashMap to hashtable (2) I guess most of the browser implement it the same, if not why not? is there any requirement how to implement it in the ECMAScript specs?

Update 2 : I understand, I just wonder how V8 and the Firefox JS VM implements the Object.properties getters/setters?

like image 789
ciochPep Avatar asked Jul 05 '11 17:07

ciochPep


People also ask

Is a JavaScript object A Hash Table?

A JavaScript Object is an example of a Hash Table because data is represented a key/value pairs. A hashing function can be used to map the key to an index by taking an input of any size and returning a hash code identifier of a fixed size.

Are JavaScript objects hash maps?

While JavaScript doesn't have a native Hashtable class, it does have native Objects and Hashmaps(Map) that offer similar functionality when it comes to organizing key/value pairs.

What is property access expression in JavaScript?

A property access expression evaluates to the value of an object property or an array element. JavaScript defines two syntaxes for property access: expression . identifier expression [ expression ] The first style of property access is an expression followed by a period and an identifier.


1 Answers

V8 doesn't implement Object properties access as hashtable, it actually implement it in a better way (performance wise)

So how does it work? "V8 does not use dynamic lookup to access properties. Instead, V8 dynamically creates hidden classes behind the scenes" - that make the access to properties almost as fast as accessing properties of C++ objects.

Why? because in fixed class each property can be found on a specific fixed offset location..

So in general accessing property of an object in V8 is faster than Hashtable..

I'm not sure how it works on other VMs

More info can be found here: https://v8.dev/blog/fast-properties

You can also read more regarding Hashtable in JS here:(my blog) http://simplenotions.wordpress.com/2011/07/05/javascript-hashtable/

like image 159
DuduAlul Avatar answered Sep 23 '22 18:09

DuduAlul