Skip to content

Commit 1585a6c

Browse files
authored
support node native es modules (#733)
1 parent 436333b commit 1585a6c

File tree

30 files changed

+352
-165
lines changed

30 files changed

+352
-165
lines changed

benchmark/complex.js

Lines changed: 0 additions & 70 deletions
This file was deleted.

benchmark/complex.mjs

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import { baseCompile } from '@intlify/message-compiler'
2+
import {
3+
translate,
4+
createCoreContext,
5+
clearCompileCache
6+
} from '@intlify/core-base'
7+
import { createI18n } from 'vue-i18n'
8+
import convertHrtime from 'convert-hrtime'
9+
import { resolve, dirname } from 'pathe'
10+
import { readJson } from './utils.mjs'
11+
12+
async function run() {
13+
const data = await readJson(resolve(dirname('.'), './benchmark/complex.json'))
14+
const len = Object.keys(data).length
15+
16+
console.log('complex pattern ...')
17+
18+
console.log(`compile time: ${len} resources`)
19+
let start = convertHrtime(process.hrtime.bigint())
20+
for (const [, source] of Object.entries(data)) {
21+
baseCompile(source)
22+
}
23+
let end = convertHrtime(process.hrtime.bigint())
24+
console.log(`ms: ${end.milliseconds - start.milliseconds}`)
25+
26+
console.log()
27+
28+
console.log(`resolve time with core: ${len} resources`)
29+
const ctx = createCoreContext({
30+
locale: 'en',
31+
modifiers: {
32+
caml: val => val
33+
},
34+
messages: {
35+
en: data
36+
}
37+
})
38+
start = convertHrtime(process.hrtime.bigint())
39+
for (const [key] of Object.entries(data)) {
40+
translate(ctx, key, 2)
41+
}
42+
end = convertHrtime(process.hrtime.bigint())
43+
console.log(`sec: ${end.seconds - start.seconds}`)
44+
console.log(`ms: ${end.milliseconds - start.milliseconds}`)
45+
46+
clearCompileCache()
47+
console.log()
48+
49+
console.log(`resolve time on composition: ${len} resources`)
50+
const i18n = createI18n({
51+
legacy: false,
52+
locale: 'en',
53+
modifiers: {
54+
caml: val => val
55+
},
56+
messages: {
57+
en: data
58+
}
59+
})
60+
start = convertHrtime(process.hrtime.bigint())
61+
for (const [key] of Object.entries(data)) {
62+
i18n.global.t(key, 2)
63+
}
64+
end = convertHrtime(process.hrtime.bigint())
65+
console.log(`ms: ${end.milliseconds - start.milliseconds}`)
66+
67+
console.log(
68+
`resolve time on composition with compile cache: ${len} resources`
69+
)
70+
start = convertHrtime(process.hrtime.bigint())
71+
for (const [key] of Object.entries(data)) {
72+
i18n.global.t(key, 2)
73+
}
74+
end = convertHrtime(process.hrtime.bigint())
75+
console.log(`ms: ${end.milliseconds - start.milliseconds}`)
76+
}
77+
78+
;(async () => {
79+
try {
80+
await run()
81+
} catch (e) {
82+
console.error(e)
83+
}
84+
})()

benchmark/index.js

Lines changed: 0 additions & 29 deletions
This file was deleted.

benchmark/index.mjs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { exec } from 'child_process'
2+
import { dirname } from 'pathe'
3+
4+
function run(pattner) {
5+
return new Promise((resolve, reject) => {
6+
exec(
7+
`node ./benchmark/${pattner}.mjs`,
8+
{ cwd: dirname('.') },
9+
(error, stdout) => {
10+
if (error) {
11+
return reject(error)
12+
}
13+
console.log(stdout)
14+
resolve()
15+
}
16+
)
17+
})
18+
}
19+
20+
;(async () => {
21+
try {
22+
for (const p of ['simple', 'complex']) {
23+
await run(p)
24+
}
25+
// await asyncForEach(['simple', 'complex'], async p => {
26+
// await run(p)
27+
// })
28+
} catch (e) {
29+
console.error(e)
30+
}
31+
})()

benchmark/simple.js

Lines changed: 0 additions & 64 deletions
This file was deleted.

benchmark/simple.mjs

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import { baseCompile } from '@intlify/message-compiler'
2+
import {
3+
translate,
4+
createCoreContext,
5+
clearCompileCache
6+
} from '@intlify/core-base'
7+
import { createI18n } from 'vue-i18n'
8+
import convertHrtime from 'convert-hrtime'
9+
import { resolve, dirname } from 'pathe'
10+
import { readJson } from './utils.mjs'
11+
12+
async function run() {
13+
const simpleData = await readJson(
14+
resolve(dirname('.'), './benchmark/simple.json')
15+
)
16+
const len = Object.keys(simpleData).length
17+
18+
console.log('simple pattern ...')
19+
20+
console.log(`compile time: ${len} resources`)
21+
let start = convertHrtime(process.hrtime.bigint())
22+
for (const [, source] of Object.entries(simpleData)) {
23+
baseCompile(source)
24+
}
25+
let end = convertHrtime(process.hrtime.bigint())
26+
console.log(`sec: ${end.seconds - start.seconds}`)
27+
console.log(`ms: ${end.milliseconds - start.milliseconds}`)
28+
29+
console.log()
30+
31+
console.log(`resolve time with core: ${len} resources`)
32+
const ctx = createCoreContext({
33+
locale: 'en',
34+
messages: {
35+
en: simpleData
36+
}
37+
})
38+
start = convertHrtime(process.hrtime.bigint())
39+
for (const [key] of Object.entries(simpleData)) {
40+
translate(ctx, key)
41+
}
42+
end = convertHrtime(process.hrtime.bigint())
43+
console.log(`ms: ${end.milliseconds - start.milliseconds}`)
44+
45+
clearCompileCache()
46+
console.log()
47+
48+
console.log(`resolve time on composition: ${len} resources`)
49+
const i18n = createI18n({
50+
legacy: false,
51+
locale: 'en',
52+
messages: {
53+
en: simpleData
54+
}
55+
})
56+
start = convertHrtime(process.hrtime.bigint())
57+
for (const [key] of Object.entries(simpleData)) {
58+
i18n.global.t(key)
59+
}
60+
end = convertHrtime(process.hrtime.bigint())
61+
console.log(`ms: ${end.milliseconds - start.milliseconds}`)
62+
63+
console.log(
64+
`resolve time on composition with compile cache: ${len} resources`
65+
)
66+
start = convertHrtime(process.hrtime.bigint())
67+
for (const [key] of Object.entries(simpleData)) {
68+
i18n.global.t(key)
69+
}
70+
end = convertHrtime(process.hrtime.bigint())
71+
console.log(`ms: ${end.milliseconds - start.milliseconds}`)
72+
}
73+
74+
;(async () => {
75+
try {
76+
await run()
77+
} catch (e) {
78+
console.error(e)
79+
}
80+
})()

benchmark/utils.mjs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import fs from 'fs/promises'
2+
3+
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
4+
export async function readJson(path) {
5+
const data = await fs.readFile(path, 'utf8')
6+
return JSON.parse(data)
7+
}

0 commit comments

Comments
 (0)