current position:Home>JS creates objects. Why is the first one better than the second? What does high performance mean, fast computing? Or does it take up less space?

JS creates objects. Why is the first one better than the second? What does high performance mean, fast computing? Or does it take up less space?

2022-05-15 04:55:23segmentfault

The first one is :

function EventHandlers() {}
EventHandlers.prototype = Object.create(null);
this._events = new EventHandlers();

The second kind :
this._events = {};

image.png




Take the answer 1:

When it comes to performance, it generally refers to time , In a broad sense, it does contain space , But generally, when there is no special explanation, it only refers to time .


But there is something wrong with what the article says , This kind of writing is not similar to obj = {} Faster than , It's the heel obj = Object.create(null) Faster than ( The English annotation on your screenshot clearly says ). Follow obj = {} Not at all 、 It must be obj = {} The slowest , The reason is simple , Because the members of the prototype chain have to be copied , There are also additional costs .

In fact, you can ignore it now , The article you read should have been released very early , This is the Node.js 6.x Pre era practice , At that time V8 Not yet Object.create Make optimization , So if you have this type, you need to use a custom empty type hacked Writing .

Actually, you're going to see Node.js 8.x Later source code , You'll find that this place has been changed back to Object.create(null) 了 .



Other answers 1:

When it comes to performance, it generally refers to time , In a broad sense, it does contain space , But generally, when there is no special explanation, it only refers to time .


But there is something wrong with what the article says , This kind of writing is not similar to obj = {} Faster than , It's the heel obj = Object.create(null) Faster than ( The English annotation on your screenshot clearly says ). Follow obj = {} Not at all 、 It must be obj = {} The slowest , The reason is simple , Because the members of the prototype chain have to be copied , There are also additional costs .

In fact, you can ignore it now , The article you read should have been released very early , This is the Node.js 6.x Pre era practice , At that time V8 Not yet Object.create Make optimization , So if you have this type, you need to use a custom empty type hacked Writing .

Actually, you're going to see Node.js 8.x Later source code , You'll find that this place has been changed back to Object.create(null) 了 .

copyright notice
author[segmentfault],Please bring the original link to reprint, thank you.
https://en.qdmana.com/2022/131/202205111331073371.html

Random recommended