golden-path
Version:
91 lines (66 loc) • 2.54 kB
JavaScript
const { PerformanceObserver, performance } = require('perf_hooks');
import { get } from '.';
let object = {
values: []
};
let timeExecution = {};
const obs = new PerformanceObserver((list) => {
const { duration } = list.getEntries()[0];
timeExecution = { duration };
});
obs.observe({ entryTypes: ['measure'] });
const OBJECT_PROPS_COUNT = 100000;
const CYCLES_COUNT = 5;
describe('performance test', () => {
beforeAll(() => {
for (let i = 0; i < OBJECT_PROPS_COUNT; i++) {
object.values.push({ name: `Lorem Ipsum ${i}` });
}
});
beforeEach(() => {
timeExecution = {};
performance.clearMarks();
});
it('should measure performance for regular access', () => {
let sum = 0;
for (let cycle = 0; cycle < CYCLES_COUNT; cycle++) {
performance.mark('Regular Property Access Start');
for (let i = 0; i < OBJECT_PROPS_COUNT; i++) {
get(`values.${i}.name`, object);
}
for (let i = 0; i < OBJECT_PROPS_COUNT; i++) {
get(`values.${i}.name`, object);
}
performance.mark('Regular Property Access End');
performance.measure(
'Regular Property Access',
'Regular Property Access Start',
'Regular Property Access End'
);
sum += timeExecution.duration;
}
expect(sum / CYCLES_COUNT).toBeLessThan(500);
console.log(`Regular Property Access AVG: ${sum / CYCLES_COUNT}`);
});
it('should measure performance for conditions access', () => {
let sum = 0;
for (let cycle = 0; cycle < CYCLES_COUNT; cycle++) {
performance.mark('Conditional Property Access Start');
for (let i = 0; i < OBJECT_PROPS_COUNT; i++) {
get(`values[name="Lorem Ipsum 100"]`, object);
}
for (let i = 0; i < 1000; i++) {
get(`values*[name="Lorem Ipsum 100"]`, object);
}
performance.mark('Conditional Property Access End');
performance.measure(
'Conditional Property Access',
'Conditional Property Access Start',
'Conditional Property Access End'
);
sum += timeExecution.duration;
}
expect(sum / CYCLES_COUNT).toBeLessThan(40);
console.log(`Conditional Property Access AVG: ${sum / CYCLES_COUNT}`);
});
});