Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Run results for:
For loop vs Regex & Reduce
Go to the benchmark
Embed
Embed Benchmark Result
Run details:
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36
Browser:
Chrome 126
Operating system:
Windows
Device Platform:
Desktop
Date tested:
one year ago
Test name
Executions per second
Using For Loop
179.6 Ops/sec
Using Regex and Reduce
193.1 Ops/sec
Tests:
Using For Loop
const generateTestText = (length) => { return "A".repeat(length).replace(/A{75}/g, "$&\n"); // Insert newline every 75 characters }; const text = generateTestText(1000000); const measurePerformance = (text) => { const charLimitPerLine = 75; const maxLines = 7; let totalLines = 0; let currentLineLength = 0; // Loop through the text and increase the total lines when a line break is found or it // has reached the char limit per line for (let i = 0; i < text.length; i++) { currentLineLength++; if (text[i] === '\n' || text[i] === '\r' || currentLineLength >= charLimitPerLine) { totalLines++; currentLineLength = 0; } } // Account any remaining character in the last line if (currentLineLength > 0) { totalLines++; } return totalLines > maxLines; }; measurePerformance(text);
Using Regex and Reduce
const generateTestText = (length) => { return "A".repeat(length).replace(/A{75}/g, "$&\n"); // Insert newline every 75 characters }; const text = generateTestText(1000000); const measurePerformance = (text) => { const charLimitPerLine = 75; const maxLines = 7; const lines = text.split(/\r?\n/); const totalLines = lines.reduce((count, line) => { return count + Math.ceil(line.length / charLimitPerLine); }, 0); return totalLines > maxLines || note.text.length > 530; } measurePerformance(text);