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;
}
function repeatifyWithArrayAndJoin(string, repetitions) {
if (repetitions < 0 || repetitions === Infinity) {
throw new RangeError('Invalid repetitions number');
}
const result = [];
for (let i = 0; i < repetitions; i++) {
result.push(string);
}
return result.join('');
}
function repeatifyWithNewArray(string, repetitions) {
if (repetitions < 0 || repetitions === Infinity) {
throw new RangeError('Invalid repetitions number');
}
return new Array(repetitions).join(string);
}
function _checkRepeations(repetitions) {
if (repetitions < 0 || repetitions === Infinity) {
throw new RangeError('Invalid repetitions number');
}
}
function repeatifyWithNewArrayAndExternalThrow(string, repetitions) {
_checkRepeations(repetitions);
return new Array(repetitions).join(string);
}
function repeatifyWithNewArrayAndExternalThrowWithProps(string, repetitions) {
this.checkRepeations(repetitions);
return new this.Array(repetitions).join(string);
}
var ctx = {
checkRepeations: _checkRepeations,
Array: Array
}
repeatifyWithNewArrayAndExternalThrowWithProps = repeatifyWithNewArrayAndExternalThrowWithProps.bind(ctx);
var TEST_STRING = 'home, sweet home';
var COUNT = 1e6;
TEST_STRING.repeat(COUNT);
repeatify(TEST_STRING, COUNT);
repeatifyWithArrayAndJoin(TEST_STRING, COUNT);
repeatifyWithNewArray(TEST_STRING, COUNT);
repeatifyWithNewArrayAndExternalThrow(TEST_STRING, COUNT);
repeatifyWithNewArrayAndExternalThrowWithProps(TEST_STRING, COUNT);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
native repeat | |
stupid repeatify | |
repeatifyWithArrayAndJoin | |
repeatifyWithNewArray | |
repeatifyWithNewArrayAndExternalThrow | |
repeatifyWithNewArrayAndExternalThrowWithProps |
Test name | Executions per second |
---|---|
native repeat | 2025898.0 Ops/sec |
stupid repeatify | 10.2 Ops/sec |
repeatifyWithArrayAndJoin | 12.5 Ops/sec |
repeatifyWithNewArray | 58.9 Ops/sec |
repeatifyWithNewArrayAndExternalThrow | 60.6 Ops/sec |
repeatifyWithNewArrayAndExternalThrowWithProps | 61.8 Ops/sec |
Taking a deep breath...
Measuring JavaScript performance is crucial, and MeasureThat.net provides a great platform for benchmarking various approaches.
Benchmark Definition JSON
The provided Benchmark Definition JSON defines a simple string repetition task. The script preparation code consists of four different implementations of the repeatify
function with varying levels of complexity:
*
) to repeat the input string.join()
method to repeat the input string.new Array(repetitions)
constructor.Individual Test Cases
Each test case corresponds to one of the above implementations, with a unique name:
native repeat
: The built-in string multiplication operator approach.stupid repeatify
: A simple loop-based concatenation implementation.repeatifyWithArrayAndJoin
: Uses an array and join()
for repetition.repeatifyWithNewArray
: Creates a new array with repeated elements.repeatifyWithNewArrayAndExternalThrow
: Similar to the previous implementation, but uses an external function.Benchmark Results
The latest benchmark results show the performance of each test case across different devices and browsers. The results are expressed in executions per second (ExecutionsPerSecond
).
Pros and Cons
Here's a brief summary of the pros and cons for each approach:
stupid repeatify
: Simple to implement but can lead to performance issues with large input strings due to string concatenation overhead.repeatifyWithArrayAndJoin
: Efficient and scalable, using array operations under the hood. However, it may have a slight performance hit compared to native repeat.repeatifyWithNewArray
: Creates a new array for each repetition, leading to memory allocation and deallocation overhead. This approach can be less efficient than others.Recommendations
Based on the benchmark results, repeatifyWithArrayAndJoin
appears to be the most efficient implementation, offering a good balance between performance and scalability. However, the built-in string multiplication operator (native repeat
) remains a close second.
When choosing an implementation for your specific use case, consider factors such as:
native repeat
might be sufficient.repeatifyWithArrayAndJoin
.repeatifyWithArrayAndJoin
is a good choice.Keep in mind that these are general guidelines, and the optimal approach may vary depending on your specific requirements.