<!--your preparation HTML code goes here-->
/*your preparation JavaScript code goes here
To execute async code during the script preparation, wrap it as function globalMeasureThatScriptPrepareFunction, example:*/
async function globalMeasureThatScriptPrepareFunction() {
// This function is optional, feel free to remove it.
// await someThing();
}
const generateChannelRanges = (startFrequency, channelWidth, count) => {
return Array.from({ length: count }, (_, i) => {
const start = startFrequency + i * channelWidth;
return { start, end: start + channelWidth };
});
};
const channelRanges = {
320: [generateChannelRanges(5945, 320, 3), generateChannelRanges(6105, 320, 3)].sort(
(a, b) => a.start - b.start
),
240: generateChannelRanges(5490, 240, 1),
160: [
generateChannelRanges(5170, 160, 1),
generateChannelRanges(5490, 160, 1),
generateChannelRanges(5945, 160, 7),
],
80: [
generateChannelRanges(5170, 80, 2),
generateChannelRanges(5490, 80, 3),
generateChannelRanges(5735, 80, 1),
generateChannelRanges(5945, 80, 14),
],
40: [
generateChannelRanges(2402, 40, 2),
generateChannelRanges(5170, 40, 4),
generateChannelRanges(5490, 40, 6),
generateChannelRanges(5735, 40, 2),
generateChannelRanges(5945, 40, 29),
],
20: [
generateChannelRanges(2402, 20, 4),
generateChannelRanges(5170, 20, 8),
generateChannelRanges(5490, 20, 12),
generateChannelRanges(5735, 20, 5),
generateChannelRanges(5925, 20, 60),
],
};
const generateChannelRanges = (startFrequency, channelWidth, count) => {
let frequency = startFrequency;
const ranges = [];
for (let i = 0; i < count; i++) {
ranges.push({ start: frequency, end: frequency + channelWidth });
frequency += channelWidth;
}
return ranges;
};
const channelRanges = {
320: [generateChannelRanges(5945, 320, 3), generateChannelRanges(6105, 320, 3)].sort(
(a, b) => a.start - b.start
),
240: generateChannelRanges(5490, 240, 1),
160: [
generateChannelRanges(5170, 160, 1),
generateChannelRanges(5490, 160, 1),
generateChannelRanges(5945, 160, 7),
],
80: [
generateChannelRanges(5170, 80, 2),
generateChannelRanges(5490, 80, 3),
generateChannelRanges(5735, 80, 1),
generateChannelRanges(5945, 80, 14),
],
40: [
generateChannelRanges(2402, 40, 2),
generateChannelRanges(5170, 40, 4),
generateChannelRanges(5490, 40, 6),
generateChannelRanges(5735, 40, 2),
generateChannelRanges(5945, 40, 29),
],
20: [
generateChannelRanges(2402, 20, 4),
generateChannelRanges(5170, 20, 8),
generateChannelRanges(5490, 20, 12),
generateChannelRanges(5735, 20, 5),
generateChannelRanges(5925, 20, 60),
],
};
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
array.from mapped fn | |
old impl |
Test name | Executions per second |
---|---|
array.from mapped fn | 241746.8 Ops/sec |
old impl | 341066.7 Ops/sec |
The JSON provided outlines a benchmark that tests different implementations of generating channel ranges based on specified frequency values. There are two implementations compared, and they utilize different methodologies to achieve the same outcome.
array.from mapped fn
: This implementation utilizes Array.from
to create an array of objects representing channel ranges. It leverages a mapped function to produce each range by calculating the start frequency and adding the channel width.old impl
: This implementation uses a traditional for
loop to generate the channel ranges. It initializes a frequency variable and iterates through the count, pushing each range into an array directly.Array.from
method, showcasing a more modern coding style.for
loop, which may be more familiar to programmers from older JavaScript versions or those used to imperative programming styles.array.from mapped fn
(First Implementation):
old impl
(Second Implementation):
old impl
has a higher execution speed, with 341,066.72 executions per second, compared to the array.from mapped fn
with 241,746.81 executions per second. This demonstrates that, in this scenario, the traditional loop is more efficient than leveraging higher-order functions like Array.from
.When evaluating performance, it's crucial to consider the specific context and environment in which the code runs. The efficacy of each implementation may vary across different browsers and devices. Additionally, readability and maintainability are essential factors. In practice, the choice of which implementation to use may depend on the specific requirements of the project, developer familiarity, and performance characteristics observed through testing.
There are alternative approaches for generating the channel ranges:
map
on a new array: Create an array of indices and then map over it to construct the ranges.while
loops, which offer different control flow characteristics.In summary, the choice between different array generation methods can have significant performance implications, alongside considerations for code clarity and maintenance. Understanding the context of use for each approach helps in making informed decisions in software development.