Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Data duplication (single run)
(version: 0)
Comparing performance of:
Build function vs New function vs JSON parse
Created:
4 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
const loremWords = [ 'lorem', 'ipsum', 'dolor', 'sit', 'amet', 'consectetur', 'adipiscing', 'elit', 'nunc', 'id', 'hendrerit', 'arcu', 'curabitur', 'vitae', 'euismod', 'justo', 'cras', 'libero', 'dui', 'scelerisque', 'a', 'suscipit', 'et', 'bibendum', 'nec', 'est', 'in', 'maximus', 'purus', 'ullamcorper', 'nulla', 'blandit', 'cursus', 'nullam', 'sapien', 'ex', 'rhoncus', 'at', 'auctor', 'vel', 'scelerisque', 'auctor', 'diam', 'cras', 'porta', 'lorem', 'non', 'efficitur', 'pulvinar', 'massa' ]; const generateLorem = () => { const amount = 5 + Math.round(Math.random() * (loremWords.length - 5)); const joined = loremWords.slice(0, amount).join(' ') + '.'; return joined[0].toUpperCase() + joined.slice(1); }; const buildItem = (createdAt, id) => ({ id, createdAt, title: `Article ${id + 1}`, text: generateLorem(), }); var makeExampleData = () => { const bool = { true: true, false: false, yes: true, no: false, 1: true, 0: false, }; const articles = Array(1000).fill(Date.now()).map(buildItem); const numbers = Array(1000).fill(100).map((x, i) => x * i); const words = Array(1000).fill(loremWords).flat(); const text = Array(1000).fill(loremWords).flat().join(' '); return {bool, articles, numbers, words, text}; };
Tests:
Build function
const buildData = () => makeExampleData(); const newData = buildData();
New function
const data = makeExampleData(); const json = JSON.stringify(data); const newData = new Function('json', `return json`)(json);
JSON parse
const data = makeExampleData(); const json = JSON.stringify(data); const newData = JSON.parse(json);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Build function
New function
JSON parse
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 benchmark and its results. **Benchmark Definition** The benchmark is designed to test three different approaches for building data: 1. **Build function**: This approach uses a self-invoking anonymous function (`const buildData = () => makeExampleData();`) to generate example data. 2. **New function**: This approach uses the `new Function()` constructor to execute a string as a JavaScript expression. 3. **JSON parse**: This approach uses `JSON.parse()` to deserialize a JSON string into an object. **Options being compared** The benchmark is comparing the execution time of each approach: * **Build function**: The self-invoking anonymous function generates example data and returns it. * **New function**: A string representing the `makeExampleData()` function is passed to `new Function()`, which executes the code and returns the result. * **JSON parse**: A JSON string representation of the generated data is passed to `JSON.parse()`, which deserializes the string into an object. **Pros and cons of each approach** 1. **Build function**: * Pros: Efficient, as it generates data directly in JavaScript. * Cons: Requires generating data upfront, which can be resource-intensive. 2. **New function**: * Pros: Can execute arbitrary code, but may have security risks if the input is not sanitized. * Cons: Slower than the build function, as `new Function()` needs to parse and execute the code. 3. **JSON parse**: * Pros: Faster and more memory-efficient than the new function approach. * Cons: Requires generating data upfront as a JSON string, which can be resource-intensive. **Library usage** None of the benchmark options explicitly use libraries for any specific functionality beyond what's built into JavaScript itself (e.g., `Array`, `Object`, etc.). **Special JS features or syntax** The benchmark uses the following special features: * Self-invoking anonymous functions (`const buildData = () => makeExampleData();`) * The `new Function()` constructor * `JSON.parse()` and `JSON.stringify()` These features are part of the standard JavaScript library, but they do introduce some complexity to the benchmark. **Alternatives** Other approaches for building data might include: 1. **Template literals**: Instead of generating data as a string, using template literals (e.g., `${expression}`) could be used. 2. **Async functions**: Using async/await syntax to generate data could improve performance by allowing non-blocking I/O operations. 3. **Web workers**: Offloading computation to web workers could provide additional performance benefits by utilizing multiple CPU cores. However, the specific approach of using a self-invoking anonymous function, `new Function()`, and `JSON.parse()` is designed to test the trade-offs between execution time, memory usage, and code complexity in generating data for building purposes.
Related benchmarks:
Unique via Set vs Filter
Data duplication
Data duplication methods
Javascript switch vs dict
Comments
Confirm delete:
Do you really want to delete benchmark?