Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
member lookup via prototype 2
(version: 0)
Comparing performance of:
Recursive vs Object.create
Created:
7 years ago
by:
Guest
Jump to the latest result
Tests:
Recursive
const getFunction = ( name, obj ) => { const proto = Object.getPrototypeOf( obj ); if( typeof proto !== "object" ) return; // console.log( proto ); return proto[ name ] || getFunction( name, proto ) // const value = proto[ key ]; // if( value !== undefined ) // return value; // return getProtoProp( key )( proto ); } getFunction( "then", Promise.resolve( 5 ) );
Object.create
const getFunction = ( name, obj ) => { const proto = Object.getPrototypeOf( obj ); return Object.create( proto )[ name ]; } getFunction( "then", Promise.resolve( 5 ) );
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Recursive
Object.create
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and its options. **Benchmark Definition** The benchmark definition is an object that represents the test case, including: * `Name`: A unique name for the benchmark ("member lookup via prototype 2") * `Description`: An empty string (not applicable in this case) * `Script Preparation Code`: An empty string (no script needs to be prepared before running the test) * `Html Preparation Code`: An empty string (no HTML preparation is required) **Individual Test Cases** There are two individual test cases: 1. **Recursive** * The benchmark definition is a JavaScript function named `getFunction` that takes two arguments: `name` and `obj`. It returns the value of `proto[name]` or recursively calls itself with `proto` as the new `obj`. * The function uses the `Object.getPrototypeOf` method to get the prototype of the object `obj`. * The test case creates a promise that resolves to 5, and then calls the `getFunction` with the name "then" and the promise as an argument. 2. **Object.create** * The benchmark definition is another JavaScript function named `getFunction` that takes two arguments: `name` and `obj`. It returns the result of creating a new object using the `Object.create` method with `proto` as the prototype, and then accessing the `name` property on that object. * Like in the Recursive test case, the function uses the `Object.getPrototypeOf` method to get the prototype of the object `obj`. * The test case creates a promise that resolves to 5, and then calls the `getFunction` with the name "then" and the promise as an argument. **Options Compared** The two test cases compare the performance of two different approaches: 1. **Recursive**: This approach uses recursion to traverse the prototype chain to find the value. 2. **Object.create**: This approach creates a new object using `Object.create` and then accesses the `name` property on that object. **Pros and Cons** * **Recursive**: + Pros: - Easy to understand and implement - Handles cases where the prototype chain has multiple layers + Cons: - May lead to stack overflows for deep prototype chains - Can be slower due to the overhead of recursive function calls * **Object.create**: + Pros: - Avoids potential stack overflows and memory issues with recursion - Typically faster than recursive approaches + Cons: - May require more careful handling of edge cases (e.g., when `proto` is null) - Less intuitive for developers who are not familiar with the `Object.create` method **Library Used** Neither test case uses any external libraries. The `Object.getPrototypeOf` and `Promise.resolve` methods are part of the standard JavaScript API. **Special JS Features or Syntax** There are no special features or syntax used in these benchmark definitions that would require additional explanation. **Other Alternatives** If you wanted to add more alternatives to this benchmark, some options could include: * Using a closure to create a new scope for each recursive call * Utilizing `Set.prototype.has` instead of accessing properties on objects * Comparing the performance of different types of objects (e.g., primitive values vs. objects) However, these alternatives would likely require significant changes to the benchmark definitions and may not be relevant to this specific use case. I hope this explanation helps you understand the provided benchmark!
Related benchmarks:
closure lookup
member lookup via prototype
Class vs Prototype Performance
object creation+method lookup: new, object.create, literal+proto
Comments
Confirm delete:
Do you really want to delete benchmark?