Skip to content

Commit

Permalink
performance improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
aeltorio committed Nov 3, 2024
1 parent 6deaeeb commit 7a03ab3
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions username_generator/src/components/GeneratorCard.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
</div>
<div class="px-6 pt-4 pb-2">
<button class="m-1 text-sm bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded"
@click="firstName = normalize(randomFirstnamePonderated(firstNames))">Change first name</button>
@click="firstName = normalize(randomFirstnamePonderated(firstNames,preComputedTotalFirstnames))">Change first name</button>
<button class="m-1 text-sm bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded"
@click="lastName = normalize(randomLastnamePonderated(lastNames))">Change last name</button>
@click="lastName = normalize(randomLastnamePonderated(lastNames,preComputedTotalLastnames))">Change last name</button>
<button class="m-1 text-sm bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded"
@click="generateUsername">Generate username</button>
</div>
Expand All @@ -20,14 +20,20 @@

<script setup lang="ts">
import { onMounted, ref } from 'vue';
import { firstNames, lastNames, normalize, randomFirstnamePonderated, randomLastnamePonderated } from '../generator';
import { firstNames, lastNames, normalize, precomputeCumulativeSums, randomFirstnamePonderated, randomLastnamePonderated } from '../generator';
const firstName = ref('');
const lastName = ref('');
const preComputedTotalFirstnames = ref(0);
const preComputedTotalLastnames = ref(0);
function generateUsername() {
firstName.value = normalize(randomFirstnamePonderated(firstNames));
lastName.value = normalize(randomLastnamePonderated(lastNames));
preComputedTotalFirstnames.value = precomputeCumulativeSums(firstNames.firstnames);
preComputedTotalLastnames.value = precomputeCumulativeSums(lastNames.lastnames);
console.log(`preComputedTotalFirstnames: ${preComputedTotalFirstnames.value}`);
console.log(`preComputedTotalLastnames: ${preComputedTotalLastnames.value}`);
firstName.value = normalize(randomFirstnamePonderated(firstNames,preComputedTotalFirstnames.value));
lastName.value = normalize(randomLastnamePonderated(lastNames,preComputedTotalLastnames.value));
}
onMounted(() => {
generateUsername();
Expand Down

0 comments on commit 7a03ab3

Please sign in to comment.