Skip to content

Commit

Permalink
Add performance test
Browse files Browse the repository at this point in the history
  • Loading branch information
tonyraoul committed Jan 17, 2025
1 parent d3b9221 commit fb24ed8
Showing 1 changed file with 143 additions and 5 deletions.
148 changes: 143 additions & 5 deletions packages/mobx/__tests__/perf/perf.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,11 +203,11 @@ results of this test:
const ar = observable([0])
const findLastIndexOfZero = computed(function () {
aCalc++
return ar.findLastIndex(x => x === 0);
return ar.findLastIndex(x => x === 0)
})
const lastIndexOfZero = computed(function () {
bCalc++
return ar.lastIndexOf(0);
return ar.lastIndexOf(0)
})
mobx.observe(findLastIndexOfZero, voidObserver, true)
mobx.observe(lastIndexOfZero, voidObserver, true)
Expand All @@ -225,9 +225,7 @@ results of this test:

const end = now()

log(
"Array findLastIndex loop - Updated in " + (end - start) + " ms."
)
log("Array findLastIndex loop - Updated in " + (end - start) + " ms.")
t.end()
})

Expand Down Expand Up @@ -552,6 +550,146 @@ results of this test:
t.end()
})

test(`${version} - Set: initializing`, function (t) {
gc()
const iterationsCount = 100000
let i

const start = Date.now()
for (i = 0; i < iterationsCount; i++) {
mobx.observable.set()
}
const end = Date.now()
log("Initilizing " + iterationsCount + " maps: " + (end - start) + " ms.")
t.end()
})

test(`${version} - Set: setting and deleting properties`, function (t) {
gc()
const iterationsCount = 1000
const propertiesCount = 10000
const set = mobx.observable.set()
let i
let p

const start = Date.now()
for (i = 0; i < iterationsCount; i++) {
for (p = 0; p < propertiesCount; p++) {
set.add("" + p)
}
for (p = 0; p < propertiesCount; p++) {
set.delete("" + p)
}
}
const end = Date.now()

log(
"Setting and deleting " +
propertiesCount +
" map properties " +
iterationsCount +
" times: " +
(end - start) +
" ms."
)
t.end()
})

test(`${version} - Set: looking up properties`, function (t) {
gc()
const iterationsCount = 1000
const propertiesCount = 10000
const set = mobx.observable.set()
let i
let p

for (p = 0; p < propertiesCount; p++) {
set.add("" + p)
}

const start = Date.now()
for (i = 0; i < iterationsCount; i++) {
for (p = 0; p < propertiesCount; p++) {
set.has("" + p)
}
}
const end = Date.now()

log(
"Looking up " +
propertiesCount +
" map properties " +
iterationsCount +
" times: " +
(end - start) +
" ms."
)
t.end()
})

test(`${version} - Set: iterator helpers`, function (t) {
gc()
const iterationsCount = 1000
const propertiesCount = 10000
const set = mobx.observable.set()
let i
let p

for (p = 0; p < propertiesCount; p++) {
set.add("" + p)
}

const start = Date.now()
for (i = 0; i < iterationsCount; i++) {
set.entries().take(1)
}
const end = Date.now()

log(
"Looking up " +
propertiesCount +
" map properties " +
iterationsCount +
" times: " +
(end - start) +
" ms."
)
t.end()
})

test(`${version} - Set: conversion to array`, function (t) {
gc()
const iterationsCount = 1000
const propertiesCount = 10000
const set = mobx.observable.set()
let i
let p

for (p = 0; p < propertiesCount; p++) {
set.add("" + p)
}

const start = Date.now()
for (i = 0; i < iterationsCount; i++) {
Array.from(set.keys())
Array.from(set.values())
Array.from(set.entries())
;[...set]
}
const end = Date.now()

log(
"Looking up " +
propertiesCount +
" map properties " +
iterationsCount +
" times: " +
(end - start) +
" ms."
)
t.end()
})

test(`${version} - Map: initializing`, function (t) {
gc()
const iterationsCount = 100000
Expand Down

0 comments on commit fb24ed8

Please sign in to comment.