Skip to content

Instantly share code, notes, and snippets.

@barrybtw
Created March 17, 2023 16:10
Show Gist options
  • Save barrybtw/a57f4280617500f9ad7336cf6715b0fc to your computer and use it in GitHub Desktop.
Save barrybtw/a57f4280617500f9ad7336cf6715b0fc to your computer and use it in GitHub Desktop.
import { getCount_IndexOf } from "./indexof.ts";
import { getCount_Set } from "./set.ts";
const input =
"hello world i am very big string maybe with vowel maybe not with wovel....";
// Performance test each function 1000000 times
const start_IndexOf = Date.now();
for (let i = 0; i < 1000000; i++) {
getCount_IndexOf(input);
}
const end_IndexOf = Date.now();
console.log(`indexOf: ${end_IndexOf - start_IndexOf}ms`);
const start_Set = Date.now();
for (let i = 0; i < 1000000; i++) {
getCount_Set(input);
}
const end_Set = Date.now();
console.log(`set: ${end_Set - start_Set}ms`);
const vowels = ["a", "e", "i", "o", "u"];
export function getCount_IndexOf(arg: string): number {
const list = arg.split("");
let result = 0;
for (let index = 0, { length } = list; index < length; ++index) {
if (vowels.indexOf(list[index]) != -1) ++result;
}
return result;
}
const vowels = new Set(["a", "e", "i", "o", "u"]);
export function getCount_Set(arg: string): number {
const list = arg.split("");
let result = 0;
for (let i = 0; i < list.length; i++) {
if (vowels.has(list[i])) ++result;
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment