Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
external or internal functions
(version: 2)
Comparing performance of:
internal vs external
Created:
8 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
function setModel(modelObj) { this._model = modelObj; return this; } function isCommand(cmd, model) { return cmd && (this.id === cmd.id || this.name === cmd) && (!model || this._model === model); } function cloneCommand() { return Object.assign({}, this); } let counter = 1; function nextId() { return counter++; } function createCommandExternal(name, func, exec, opts) { const beforeId = nextId(); const afterId = nextId(); const creator = function(...args) { return { func, exec, args, opts, beforeId, afterId, isCmd: true, id: beforeId, funcName: name, context: this, model: setModel, is: isCommand, clone: cloneCommand }; }; creator.id = beforeId; creator.Before = { id: beforeId }; creator.After = { id: afterId }; return creator; } function createCommandInternal(name, func, exec, opts) { const beforeId = nextId(); const afterId = nextId(); const creator = function(...args) { return { func, exec, args, opts, beforeId, afterId, isCmd: true, id: beforeId, funcName: name, context: this, model(modelObj) { this._model = modelObj; return this; }, is(cmd, model) { return cmd && (this.id === cmd.id || this.name === cmd) && (!model || this._model === model); }, clone() { return Object.assign({}, this); }, appendArgs(cmd, args) { cmd = ensureCmdObject(cmd); cmd.args = cmd.args.concat(args); }, setContext(cmd, ctx) { cmd = ensureCmdObject(cmd); cmd.context = ctx; cmd.name = getCommandName(cmd.funcName, ctx); return cmd; }, hash(cmd) { if (is.func(cmd)) { return `${cmd.id}`; } else { cmd = ensureCmdObject(cmd); const argsHash = cmd.args.length > 0 ? JSON.stringify(cmd.args) : ""; return `${cmd.id}${argsHash}`; } } }; }; creator.id = beforeId; creator.Before = { id: beforeId }; creator.After = { id: afterId }; return creator; }
Tests:
internal
createCommandInternal('testCommand', () => 'hello', {}, {})
external
createCommandExternal('testCommand', () => 'hello', {}, {})
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
internal
external
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 Definition JSON and explain what's being tested. **Benchmark Definition** The benchmark defines two types of command creators: `createCommandInternal` and `createCommandExternal`. Both functions create objects with similar properties, but they differ in how the object is constructed. 1. **createCommandInternal**: This function creates a command object internally by defining all its properties directly on the `creator` function. 2. **createCommandExternal**: This function creates a command object externally by returning an object created by another function (`creator`) that has these properties defined elsewhere. **Options Compared** The benchmark compares two options: 1. **Internal Command Creation (createCommandInternal)**: In this approach, all properties are defined directly on the `creator` function. 2. **External Command Creation (createCommandExternal)**: In this approach, a separate function (`creator`) is used to create an object with these properties. **Pros and Cons** **Internal Command Creation (createCommandInternal)** Pros: * May be faster since no external function call is involved * Could be more cache-friendly since the `creator` function is likely to be inlined by the compiler Cons: * Less modular, making it harder to reuse or modify individual components * Might lead to tighter coupling between functions **External Command Creation (createCommandExternal)** Pros: * More modular, making it easier to reuse or modify individual components * Allows for better separation of concerns and reusability Cons: * May be slower due to the external function call * Could result in more cache misses since the `creator` object is created outside its primary context. **Other Considerations** * **Library Usage**: The benchmark uses a library-like approach with `Object.assign()` and `ensureCmdObject()`, which might not be applicable to all scenarios. Make sure the chosen implementation aligns with your specific use case. * **Device-Specific Optimizations**: Both benchmarks are executed on Chrome 59, but it's essential to test them across different browsers and devices to ensure they provide consistent performance. **Special JS Features or Syntax** There are no explicit mentions of special JavaScript features or syntax in the benchmark definition. However, the use of `Object.assign()` and `ensureCmdObject()` might involve some level of feature-specific optimizations (e.g., support for older browsers). **Alternatives** To explore alternative approaches, consider the following options: 1. **Hybrid Approach**: Combine elements of both internal and external command creation to strike a balance between performance and modularity. 2. **Alternative Library or Framework**: Investigate if there are other libraries or frameworks that can simplify the task of creating command objects. 3. **Native Implementation**: Consider implementing the command creator logic natively, potentially using browser-specific APIs or WebAssembly (WASM) for better performance. When selecting an alternative approach, keep in mind your specific requirements, such as performance constraints, code modularity, and maintainability needs. Feel more at ease?
Related benchmarks:
Object Deep Copy with jQuery
Object Deep Copyit
ES6 vs Lodash object copying
JavaScript fastest way to clone an object + JSON copy
Comments
Confirm delete:
Do you really want to delete benchmark?