function repeatify(string, repetitions) {
if (repetitions < 0 || repetitions === Infinity) {
throw new RangeError('Invalid repetitions number');
}
let result = '';
for (let i = 0; i < repetitions; i++) {
result += string;
}
return result;
}
repeatify('*', 10000);
function repeatify2(string, repetitions) {
if (repetitions < 0 || repetitions === Infinity) {
throw new RangeError('Invalid repetitions number');
}
const isEven = repetitions % 2 === 0;
const iterations = Math.floor(repetitions / 2);
const stringTwice = string + string;
let result = '';
for (let i = 0; i < iterations; i++) {
result += stringTwice;
}
if (!isEven) {
result += string;
}
return result;
}
repeatify2('*', 10000);
function repeatify3(string, repetitions) {
if (repetitions < 0 || repetitions === Infinity) {
throw new RangeError('Invalid repetitions number');
}
const cache = new Map();
function repeat(string, repetitions) {
if (repetitions === 0) {
return '';
}
const log = Math.floor(Math.log2(repetitions));
let result;
if (cache.has(log)) {
result = cache.get(log);
} else {
result = string;
for (let i = 1; i <= log; i++) {
result += result;
cache.set(i, result);
}
}
const repetitionsProcessed = Math.pow(2, log);
const repetitionsLeft = repetitions - repetitionsProcessed;
return result + repeat(string, repetitionsLeft);
}
return repeat(string, repetitions);
}
repeatify3('*', 10000);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Simple for loop | |
Extended for loop | |
Log based |
Test name | Executions per second |
---|---|
Simple for loop | 16449.4 Ops/sec |
Extended for loop | 32179.8 Ops/sec |
Log based | 409961.4 Ops/sec |
Let's dive into the world of JavaScript microbenchmarks and analyze the provided benchmark definitions.
Benchmark Definition The benchmark definition is a JSON object that contains information about the test case, including the name, description, script preparation code, and HTML preparation code. In this case, there are three benchmark definitions:
Test Cases
Each test case represents a specific implementation of the repeating string function, which takes two arguments: string
and repetitions
. The function is expected to return a new string by repeating the original string
a specified number of times.
This test case uses a traditional for
loop to repeat the string.
function repeatify(string, repetitions) {
if (repetitions < 0 || repetitions === Infinity) {
throw new RangeError('Invalid repetitions number');
}
let result = '';
for (let i = 0; i < repetitions; i++) {
result += string;
}
return result;
}
Pros:
Cons:
This test case uses a similar for
loop, but with an added twist. It checks if the number of repetitions is even and adds an extra repetition of the string if it's not.
function repeatify2(string, repetitions) {
if (repetitions < 0 || repetitions === Infinity) {
throw new RangeError('Invalid repetitions number');
}
const isEven = repetitions % 2 === 0;
const iterations = Math.floor(repetitions / 2);
const stringTwice = string + string;
let result = '';
for (let i = 0; i < iterations; i++) {
result += stringTwice;
}
if (!isEven) {
result += string;
}
return result;
}
Pros:
stringTwice
variableCons:
isEven
flag and iterations
variable)This test case uses a logarithmic approach to repeat the string.
function repeatify3(string, repetitions) {
if (repetitions < 0 || repetitions === Infinity) {
throw new RangeError('Invalid repetitions number');
}
const cache = new Map();
function repeat(string, repetitions) {
if (repetitions === 0) {
return '';
}
const log = Math.floor(Math.log2(repetitions));
let result;
if (cache.has(log)) {
result = cache.get(log);
} else {
result = string;
for (let i = 1; i <= log; i++) {
result += result;
cache.set(i, result);
}
}
const repetitionsProcessed = Math.pow(2, log);
const repetitionsLeft = repetitions - repetitionsProcessed;
return result + repeat(string, repetitionsLeft);
}
return repeat(string, repetitions);
}
Pros:
Cons:
cache
map) and complex logicIn summary, each test case has its pros and cons. The "Log based" approach is likely the fastest due to optimized loop iterations, but it introduces additional complexity and data structures. The "Extended for loop" adds a small optimization by using the stringTwice
variable, but may lead to increased complexity. The "Simple for loop" is straightforward but may be slower due to repeated calculations.
Browser Results The provided browser results show that:
These results suggest that the "Log based" approach is indeed the fastest, but further testing and optimization may be necessary to achieve optimal performance.