I have the following script in my VueJs2 Store Config, and I suspect that the if statement below is causing extra load, even if not triggered, but I’m unsure:
JavaScript
13
1
if(response.status === 204) {
2
addToastMessage(I18n.t('library.withdrawal.errors.already_noted'), 'is-info');
3
createDiv();
4
const btn = document.createElement("button");
5
addBtnAttributes(btn);
6
btn.addEventListener("click", function()
7
{
8
commit('changeBookLoading', true);
9
fetchNextTicket(response);
10
commit('changeBookLoading', false);
11
});
12
}
13
Being new to testing in JS in general, I would like to hear suggestions as to the best methods of benchmarking and debugging this further, any tips appreciated.
Full function:
JavaScript
33
1
function loadBook(commit, dispatch, method, id=null) {
2
commit('changeBookLoading', true);
3
dispatch(method, id)
4
.then((response) => {
5
6
if(response.status === 204) {
7
addToastMessage(I18n.t('library.withdrawal.errors.already_noted'), 'is-info');
8
createDiv();
9
const btn = document.createElement("button");
10
addBtnAttributes(btn);
11
btn.addEventListener("click", function()
12
{
13
commit('changeBookLoading', true);
14
fetchNextTicket(response);
15
commit('changeBookLoading', false);
16
});
17
}
18
const bookId = response.data[0].localBook.id;
19
20
if (bookId !== locationBookId()) {
21
history.pushState({}, `Book ${bookId}`, `/lib/book/${bookId}`);
22
}
23
24
commit('changeCurrentBookId', ticketId);
25
commit('changeBookEi', null);
26
commit('changeCurrentBook', null);
27
})
28
.catch((err) => {
29
// show some global error
30
})
31
.finally(() => commit('changeLibraryLoading', false));
32
}
33
Advertisement
Answer
You can measure time of the execution of any block of code with:
JavaScript
5
1
console.time('some measure');
2
3
4
console.timeEnd('some measure');
5
And you will got console output like: some measure: 0.0126953125 ms You just need to put you code between time and timeEnd calls.