Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
member lookup via prototype
(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( ! ( proto instanceof 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 ); if( ! ( proto instanceof Object ) ) return; return Object.create( proto )[ name ]; // 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 ) );
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 JSON and explain what is being tested, the options being compared, their pros and cons, and other considerations. **Benchmark Definition** The provided benchmark definition is essentially a JavaScript function that tests two different approaches to accessing properties on objects in the prototype chain. The `getFunction` function takes an object (`obj`) as its first argument and a property name (`name`) as its second argument. It returns the value of the specified property on the given object. **Option 1: Recursive** The "Recursive" test case uses the following implementation: ```javascript const getFunction = (name, obj) => { const proto = Object.getPrototypeOf(obj); if (!proto instanceof Object) return; // console.log(proto); // Commented out in the original code return proto[name] || getFunction(name, proto); } ``` This approach traverses the prototype chain recursively to find the property value. The function calls itself with the next prototype object until it reaches an empty prototype (i.e., `null` or `undefined`). If the object is not in the prototype chain, it returns `undefined`. **Pros:** * Handles objects with a large number of prototypes * Can be efficient for small to medium-sized objects **Cons:** * May be slower than other approaches due to recursive function calls * Can lead to stack overflow errors if the object has an extremely deep prototype chain **Option 2: Object.create** The "Object.create" test case uses the following implementation: ```javascript const getFunction = (name, obj) => { const proto = Object.getPrototypeOf(obj); return Object.create(proto)[name]; } ``` This approach creates a new object using `Object.create` and sets its prototype to the given prototype (`obj`). It then accesses the specified property on this new object. **Pros:** * Faster than recursive approaches due to avoided function calls * Can handle large numbers of prototypes without stack overflow errors **Cons:** * May not work correctly if the original object has a different length property (e.g., `NaN`) * May lead to memory leaks if used excessively **Library and Special Features** There is no explicit library mentioned in the benchmark definition. However, it's worth noting that `Object.getPrototypeOf` and `Object.create` are built-in JavaScript methods. **Other Considerations** When writing benchmarks like this one, consider the following: * Use a consistent test framework to ensure repeatability. * Avoid using external libraries or modules unless necessary. * Optimize code for performance and memory usage where possible. * Include multiple test cases to cover different scenarios and edge cases. As for alternatives, some other approaches to accessing properties on objects in the prototype chain could include: * Using a loop instead of recursion * Using `Object.getPrototypeOf` with a custom implementation (e.g., avoiding `instanceof` checks) * Using a specialized library or module designed for working with prototypes Keep in mind that these alternatives may have their own pros and cons, and it's essential to evaluate them based on the specific use case and requirements.
Related benchmarks:
closure lookup
member lookup via prototype 2
Class instance method lookup vs function-created object method lookup
Class vs Prototype Performance
Comments
Confirm delete:
Do you really want to delete benchmark?