var input = { foo: 'lorem', bar: 'ipsum', baz: null, qux: 'dosol', quz: 'set'}
var reduce = (itemsArray, callback, seed) => {
let accumulator = seed;
for (let i = 0; i < itemsArray.length; i += 1) {
accumulator = callback(accumulator, itemsArray[i], i);
}
return accumulator;
};
var allowedKeys = ['foo', 'bar', 'baz'];
function renderAttribute(key, value) {
return value ? `data-analytics-view-custom-analytics-${key}="${value}"` : '';
}
const keys = ['foo', 'bar', 'baz'];
const setProperty = (acc, key, value) => value ? (acc[key] = value) && acc : acc;
const result = keys.reduce((acc, key) => setProperty(acc, key, input[key]), {});
Object.entries(result).map(
([key, value]) => `data-analytics-view-custom-analytics-${key}="${value}"`,
).join(' ')
const keys = ['foo', 'bar', 'baz'];
const setProperty = (acc, key, value) => value ? (acc[key] = value) && acc : acc;
const result = reduce(
keys,
(acc, key) => setProperty(acc, key, input[key]),
{}
);
Object.entries(result).map(
([key, value]) => `data-analytics-view-custom-analytics-${key}="${value}"`,
).join(' ')
const { foo, bar, baz } = input;
const result = {};
if (foo) {
result.foo = foo;
}
if (bar) {
result.bar = bar;
}
if (baz) {
result.baz = baz;
}
Object.entries(result).map(
([key, value]) => `data-analytics-view-custom-analytics-${key}="${value}"`,
).join(' ')
const { foo, bar, baz } = input;
const result = { foo, bar, baz };
Object.entries(result).map(
([key, value]) => value ? `data-analytics-view-custom-analytics-${key}="${value}"` : '',
).join(' ')
const { foo, bar, baz } = input;
const result = Object.entries({ foo, bar, baz }).filter(([key, value]) => value !== null && value !== undefined);
result.map(
([key, value]) => `data-analytics-view-custom-analytics-${key}="${value}"`,
).join(' ')
reduce(
Object.entries(input),
(acc, [key, value]) => (~allowedKeys.indexOf(key) && value) ? `${acc} data-analytics-view-custom-analytics-${key}="${value}"` : acc,
''
);
Object.entries(input).reduce(
(acc, [key, value]) => (~allowedKeys.indexOf(key) && value) ? `${acc} data-analytics-view-custom-analytics-${key}="${value}"` : acc,
''
)
const { foo, bar, baz } = input;
const html = `
${foo ? `data-analytics-view-custom-analytics-foo="${foo}"` : ''}
${bar ? `data-analytics-view-custom-analytics-bar="${bar}"` : ''}
${baz ? `data-analytics-view-custom-analytics-baz="${baz}"` : ''}
`
const { foo, bar, baz } = input;
const attributeName = 'data-analytics-view-custom-analytics-';
const html = `
${foo ? `${attributeName}-foo="${foo}"` : ''}
${bar ? `${attributeName}-bar="${bar}"` : ''}
${baz ? `${attributeName}-baz="${baz}"` : ''}
`
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Reduce | |
Looping | |
Ifs | |
Destructuring | |
Object.entries | |
Single loop | |
Single reduce | |
Template string | |
Template shared |
Test name | Executions per second |
---|---|
Reduce | 734596.3 Ops/sec |
Looping | 613031.8 Ops/sec |
Ifs | 1130539.2 Ops/sec |
Destructuring | 1006708.9 Ops/sec |
Object.entries | 698095.5 Ops/sec |
Single loop | 601365.2 Ops/sec |
Single reduce | 683619.2 Ops/sec |
Template string | 7540458.0 Ops/sec |
Template shared | 7294179.5 Ops/sec |
The code snippet provided appears to be part of a benchmarking script, likely written in JavaScript, that measures the performance of different template-related functions.
Here's a succinct summary of the code:
Benchmark Configuration
const benchmarkConfig = [
{
"attributeName": "template-string",
"rawUAString": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.111 Safari/537.36"
},
{
"attributeName": "template-shared",
"rawUAString": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.111 Safari/537.36"
},
// ... other benchmark configurations ...
];
Benchmark Data
const benchmarkData = [
{
"RawUAString": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.111 Safari/537.36",
"Browser": "Chrome 86",
"DevicePlatform": "Desktop",
"OperatingSystem": "Windows",
"ExecutionsPerSecond": 7540458.0,
"TestName": "Template string"
},
// ... other benchmark data ...
];
Pre-ambles and Template Strings
The code snippet defines a function that generates template strings with bar
and baz
attributes, like this:
${attributeName}-bar=\"${bar}\"}
${attributeName}-baz=\"${baz}\"}
This suggests that the benchmarking script is comparing the performance of different template-related functions, such as template strings and shared templates.
Skip Preambles
The comment at the top of the code snippet mentions skipping preambles. This likely refers to a feature in the benchmarking tool that allows users to skip or modify certain sections of the output.
Overall, this code snippet appears to be part of a larger benchmarking script that measures the performance of template-related functions in JavaScript.