-
-
Notifications
You must be signed in to change notification settings - Fork 238
/
Copy pathmemory.js
executable file
·62 lines (48 loc) · 1.76 KB
/
memory.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
const { join } = require('path')
const { readFileSync } = require('fs')
const { getHeapStatistics } = require('v8')
const { Liquid } = require('..')
const engineOptions = {
root: __dirname,
extname: '.liquid'
}
const engine = new Liquid(engineOptions)
const SAMPLE_COUNT = 1024
function memory () {
console.log(' memory')
console.log('------------------------')
html()
todolist()
}
function html () {
const str = readFileSync(join(__dirname, 'templates/lorem-html.liquid'), 'utf8')
global.gc()
const base = getHeapStatistics().used_heap_size
const templates = []
for (let i = 0; i < SAMPLE_COUNT; i++) {
templates.push(engine.parse(str))
}
const diff1 = (getHeapStatistics().used_heap_size - base) / SAMPLE_COUNT
console.log(`[lorem-html ${h(str.length)}][before GC] ${h(diff1)}/tpl (${SAMPLE_COUNT} runs sampled)`)
global.gc()
const diff2 = (getHeapStatistics().used_heap_size - base) / SAMPLE_COUNT
console.log(`[lorem-html ${h(str.length)}][after GC] ${h(diff2)}/tpl (${SAMPLE_COUNT} runs sampled)`)
}
function todolist () {
const str = readFileSync(join(__dirname, 'templates/todolist.liquid'), 'utf8')
global.gc()
const base = getHeapStatistics().used_heap_size
const templates = []
for (let i = 0; i < SAMPLE_COUNT; i++) {
templates.push(engine.parse(str))
}
const diff1 = (getHeapStatistics().used_heap_size - base) / SAMPLE_COUNT
console.log(`[todolist ${h(str.length)}][before GC] ${h(diff1)}/tpl (${SAMPLE_COUNT} runs sampled)`)
global.gc()
const diff2 = (getHeapStatistics().used_heap_size - base) / SAMPLE_COUNT
console.log(`[todolist ${h(str.length)}][after GC] ${h(diff2)}/tpl (${SAMPLE_COUNT} runs sampled)`)
}
function h (size) {
return (size / 1024).toFixed(3) + ' KB'
}
module.exports = { memory }