Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
JS new obj. destruct vs fields
(version: 0)
Comparing performance of:
fields vs destruct
Created:
2 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
function getNewObj(note) { return { id: note.id, entity_id: note.entity_id, created_by: note.created_by, updated_by: note.updated_by, created_at: note.created_at, updated_at: note.updated_at, responsible_user_id: note.responsible_user_id, group_id: note.group_id, note_type: note.note_type, params_uniq: note.params.uniq, params_duration: note.params.duration, params_source: note.params.source, params_link: note.params.link, params_phone: note.params.phone, params_call_result: note.params.call_result, params_call_status: note.params.call_status, account_id: note.account_id, } } function destructObj({ _links, params, ...props }) { return { ...props, params_uniq: params.uniq, params_duration: params.duration, params_source: params.source, params_link: params.link, params_phone: params.phone, params_call_result: params.call_result, params_call_status: params.call_status, } } const sample = { id: 334078557, entity_id: 41208705, created_by: 9016986, updated_by: 9016986, created_at: 1684479922, updated_at: 1684479921, responsible_user_id: 9016986, group_id: 176992, note_type: 'call_out', params: { uniq: '1684479899.477515', duration: 0, source: 'sipuni', link: null, phone: 'Алексей', call_result: 'Не дозвонились', call_status: 6, }, account_id: 14085820, _links: { self: { href: 'https://amocrm.ru', }, }, }
Tests:
fields
const sample = { id: 334078557, entity_id: 41208705, created_by: 9016986, updated_by: 9016986, created_at: 1684479922, updated_at: 1684479921, responsible_user_id: 9016986, group_id: 176992, note_type: 'call_out', params: { uniq: '1684479899.477515', duration: 0, source: 'sipuni', link: null, phone: 'Алексей', call_result: 'Не дозвонились', call_status: 6, }, account_id: 14085820, _links: { self: { href: 'https://amocrm.ru', }, }, } const newObj = getNewObj(sample)
destruct
const sample = { id: 334078557, entity_id: 41208705, created_by: 9016986, updated_by: 9016986, created_at: 1684479922, updated_at: 1684479921, responsible_user_id: 9016986, group_id: 176992, note_type: 'call_out', params: { uniq: '1684479899.477515', duration: 0, source: 'sipuni', link: null, phone: 'Алексей', call_result: 'Не дозвонились', call_status: 6, }, account_id: 14085820, _links: { self: { href: 'https://amocrm.ru', }, }, } const newObj = destructObj(sample)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
fields
destruct
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 and analyze what's being tested. **Benchmark Definition** The benchmark measures the performance of two approaches to creating a new JavaScript object: 1. `getNewObj(sample)` (also referred to as "new obj. destruct vs fields") 2. `destructObj(sample)` (also referred to as "fields" and "destruct") Both functions take an object `sample` as input and return a new object with some properties copied from the original. **What's being tested?** The benchmark compares the performance of two approaches: 1. **Direct object creation**: Using the `getNewObj` function, which creates a new object by destructuring the `sample` object. 2. **Object literal syntax**: Using the `destructObj` function, which uses an object literal syntax to create a new object. **Pros and Cons** 1. **Direct object creation (getNewObj)**: * Pros: Can be more readable and concise, especially when working with complex objects. * Cons: May have performance overhead due to the need to destructure the object, which can be slower than creating an object literal. 2. **Object literal syntax (destructObj)**: * Pros: Typically faster and more efficient than direct object creation, as it avoids the overhead of destructuring. * Cons: May require more boilerplate code to create a new object. **Library** None, this benchmark uses native JavaScript functions. **Special JS feature or syntax** The `...` spread operator is used in both approaches. It's a modern JavaScript feature introduced in ECMAScript 2015 that allows you to pass an array of properties to an object literal, spreading the properties onto the new object. In the case of `getNewObj`, it uses destructuring assignment (`{ ...props }`) to create a new object with the same properties as the original, except for `_links`. In `destructObj`, it uses the spread operator (`...props`) to copy the properties from the original object onto the new object. **Other considerations** When writing performance-critical code, it's essential to consider the trade-offs between readability, maintainability, and performance. In this case, the benchmark highlights the importance of choosing the most efficient approach for your specific use case. If you need more performance, using an object literal syntax like `destructObj` might be a better choice. However, if readability and conciseness are more important, direct object creation with `getNewObj` might be a better option. **Alternatives** Some alternative approaches to creating new objects in JavaScript include: 1. Using the `Object.assign()` method: This can be faster than creating an object literal or using destructuring. 2. Using the `lodash` library: This provides various functions for creating and manipulating objects, including more efficient alternatives to direct object creation. However, these alternatives might introduce additional dependencies or complexity, so it's essential to consider your specific use case and performance requirements when choosing a method.
Related benchmarks:
Delete vs destructure for objects
Delete vs destructure for cloned objects
Delete vs destructure for objects v2 2
Delete vs destructure for objects - guigo2
Delete vs destructure for objects without mutating 2
Comments
Confirm delete:
Do you really want to delete benchmark?