Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Plain Object Check: constructor vs prototype
(version: 0)
Checks whether the given object is a plain Object.
Comparing performance of:
constructor vs prototype
Created:
one year ago
by:
Registered User
Jump to the latest result
Script Preparation code:
function MyType() {}; MyType.prototype.foo = "bar"; var fixture = []; for (var idx = 0; idx < 1000; idx++) { if (Math.random() >= 0.5) { fixture.push(new MyType()); } else { fixture.push({ "foo": "bar" }); } }
Tests:
constructor
fixture.filter(function(e){ return e.constructor === Object; });
prototype
fixture.filter(function(e){ return Object.getPrototypeOf(e) === Object.prototype; });
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
constructor
prototype
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
8 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:142.0) Gecko/20100101 Firefox/142.0
Browser/OS:
Firefox 142 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
constructor
180801.2 Ops/sec
prototype
155074.9 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'd be happy to help you understand the JavaScript microbenchmark on MeasureThat.net. **Benchmark Definition** The benchmark is designed to measure the performance of two approaches for checking whether an object is a plain Object: 1. **Constructor check**: This approach uses the `constructor` property of an object to determine if it's a plain Object. 2. **Prototype check**: This approach uses the prototype chain of an object to determine if it's a plain Object. The benchmark defines two test cases: **Script Preparation Code** The script prepares a fixture array with 1000 elements, where half of them are instances of `MyType` (a custom class) and the other half are objects with a single property `foo` containing the value `"bar"`. **Html Preparation Code** There is no HTML preparation code provided for this benchmark, which means that the browser doesn't need to parse or execute any additional JavaScript code before running the benchmark. **Individual Test Cases** The two test cases compare the performance of the two approaches: 1. **Constructor check**: The benchmark definition uses a filter function to create an array of objects where each object's `constructor` property is checked against `Object`. This approach checks if the object's constructor is a function that returns `Object`. 2. **Prototype check**: The benchmark definition uses a similar filter function, but instead of checking the `constructor` property directly, it checks if the prototype of an object is equal to `Object.prototype`. **Pros and Cons** Here are some pros and cons for each approach: * **Constructor check**: + Pros: Simple and efficient. + Cons: May not work correctly for objects with a custom constructor that returns `Object`. * **Prototype check**: + Pros: Works correctly even if the object's constructor returns `Object`. + Cons: May be slower due to the additional lookup in the prototype chain. **Library** There is no explicit library used in this benchmark, but it uses the `filter` method and the `constructor` property of objects. The `filter` method is a built-in JavaScript method that applies a given function to each element of an array. **Special JS feature or syntax** This benchmark doesn't use any special JavaScript features or syntax, such as ES6 classes, async/await, or Promises. **Other Alternatives** If you want to create similar benchmarks for other checks, here are some alternative approaches: * To check if an object is a plain Object using the `constructor` property: ```javascript function isPlainObject(obj) { return obj.constructor === Function && obj.constructor.prototype === Object.prototype; } ``` * To check if an object's prototype is equal to `Object.prototype`: ```javascript function hasSamePrototype(obj) { return Object.getPrototypeOf(obj) === Object.prototype; } ``` Keep in mind that these alternatives may have different performance characteristics and edge cases compared to the original benchmark.
Related benchmarks:
Object vs toString
Check object. typeof vs constructor + null check
Check object. typeof vs constructor
javascript new vs Object.create 2
Array isArray vs Object.prototype
Comments
Confirm delete:
Do you really want to delete benchmark?