flaska/benchmark/set_arr.mjs

117 lines
2.6 KiB
JavaScript

import { summary, run, bench } from 'mitata';
// Warmup (de-optimize `bench()` calls)
bench('noop', () => { });
bench('noop2', () => { });
function padStart(length) {
return ''.padStart(length * 2)
}
const data = [
'/',
'/api/articles',
'/api/articles/:id/file',
'/api/articles/:id',
'/api/articles/public',
'/api/articles/public/:id',
'/api/categories',
'/api/categories/:categoryId/products',
'/api/categories/:categoryId/properties',
'/api/categories/:categoryId/values/:props',
'/api/categories/:categoryId',
//'/api/categories/:categoryId/products/:productId',
'/api/categories/:categoryId/products/:productId',
'/api/customers',
'/api/customers/:id',
'/api/customers/kennitala/:kennitala',
'/api/customers/public/kennitala/:kennitala',
'/api/customers/search/:search',
'/api/file',
'/api/file/:id',
'/api/media',
'/api/media/:id',
'/api/orderitem',
'/api/orderitem/:id',
'/api/orders',
'/api/orders/:orderId',
'/api/orders/:orderId/sell',
'/api/pages',
'/api/pages/:pageId',
'/api/pages/:pageId/articles',
'/api/pages/:pageId/articles/public',
'/api/products',
'/api/products/:id',
'/api/products/:id/movement',
'/api/products/:id/sub_products/:productId',
//'/api/products/:id/sub_products/:productId',
'/api/products/code/:code',
'/api/products/property/:propertyId',
'/api/properties',
'/api/properties/:id',
'/api/sales',
'/api/sales/:id',
'/api/stockitem',
'/api/stockitem/:id',
'/api/stocks',
'/api/stocks/:id',
'/api/stocks/:id/commit',
'/api/test',
'/api/test/auth',
'/api/test/error',
'/api/workentries',
'/api/workentries/:id',
'/api/works',
'/api/works/:id',
'/api/works/:id/lock',
'/api/works/public',
'/api/staff',
'/api/staff/:id',
'/::rest',
]
function arrIncludes(data) {
let out = new Array()
for (let item of data) {
if (out.includes(item)) { break }
out.push(item)
}
return out
}
function setAdd(data) {
let s = new Set()
for (let item of data) {
let size = s.size
if (s.add(item).size === size) { break }
}
return s
}
let func = [arrIncludes, setAdd];
for (let fun of func) {
console.log(`--- warming up ${fun.name || 'mapl'} ---`)
for (var i = 0; i < 100; i++) {
fun(data)
}
}
await new Promise(res => setTimeout(res, 3000))
summary(() => {
for (let i = 20; i >= 0; i--) {
const dataSet = data.slice(0, data.length - i)
func.forEach(function(fun) {
bench(`${dataSet.length} items: ${fun.name}`, function() {
return fun(dataSet)
})
})
}
// console.log(tests, fun, tests.map(fun))
/*bench(fun.name, function() {
return fun(data)
})*/
})
run();