Golang 与 JavaScript 字符串反转性能对比:百万次循环测试
const { performance } = require('perf_hooks');
function main() { const start = performance.now();
let str = 'hello, world!'; for (let i = 0; i < 10000000; i++) { str = str.replace(/o/g, '0'); str = str.replace(/l/g, '1'); str = str.replace(/,/g, ''); str = str.replace(/!/g, ''); str = reverse(str); }
const end = performance.now(); console.log('JS程序运行时间:', end - start, 'ms'); }
function reverse(s) { let runes = s.split(''); for (let i = 0, j = runes.length - 1; i < j; i++, j--) { [runes[i], runes[j]] = [runes[j], runes[i]]; } return runes.join(''); }
main();
原文地址: https://www.cveoy.top/t/topic/ofhw 著作权归作者所有。请勿转载和采集!