Skip to content

Instantly share code, notes, and snippets.

@axross
Created October 10, 2019 19:03
Show Gist options
  • Save axross/7985189f9bc4c5a359acd727e8a5826b to your computer and use it in GitHub Desktop.
Save axross/7985189f9bc4c5a359acd727e8a5826b to your computer and use it in GitHub Desktop.
import { runBenchmarks, bench } from "https://deno.land/std/testing/bench.ts";
// 1119. Remove Vowels from a String
// https://leetcode.com/problems/remove-vowels-from-a-string/
function removeVowelsByLoop(S: string): string {
let removed = "";
for (const char of S) {
switch (char) {
case "a":
case "e":
case "i":
case "o":
case "u":
break;
default:
removed += char;
}
}
return removed;
}
function removeVowelsByRegex(S: string): string {
return S.replace(/[aeiou]+/g, "");
}
const STRINGS = Array.from(new Array(100), () =>
Array.from(new Array(100000), () =>
String.fromCharCode(Math.floor(Math.random() * 26) + 97)
).join("")
);
bench({
name: "loop",
runs: 100,
func(b): void {
b.start();
for (const string of STRINGS) {
removeVowelsByLoop(string);
}
b.stop();
}
});
bench({
name: "regex",
runs: 100,
func(b): void {
b.start();
for (const string of STRINGS) {
removeVowelsByRegex(string);
}
b.stop();
}
});
runBenchmarks();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment