|
| 1 | +import { createCommonJS } from 'mlly' |
| 2 | +import { baseCompile } from '@intlify/message-compiler' |
| 3 | +import { |
| 4 | + translate, |
| 5 | + createCoreContext, |
| 6 | + compile, |
| 7 | + registerMessageCompiler, |
| 8 | + clearCompileCache |
| 9 | +} from '@intlify/core-base' |
| 10 | +import { createI18n } from 'vue-i18n' |
| 11 | +import { resolve, dirname } from 'pathe' |
| 12 | +import { readJson } from './utils.mjs' |
| 13 | + |
| 14 | +const { require } = createCommonJS(import.meta.url) |
| 15 | +const { Suite } = require('benchmark') |
| 16 | + |
| 17 | +function precompile(data) { |
| 18 | + const keys = Object.keys(data) |
| 19 | + keys.forEach(key => { |
| 20 | + const { ast } = baseCompile(data[key], { jit: true, location: false }) |
| 21 | + data[key] = ast |
| 22 | + }) |
| 23 | + return data |
| 24 | +} |
| 25 | + |
| 26 | +async function main() { |
| 27 | + const resources = await readJson( |
| 28 | + resolve(dirname('.'), './benchmark/complex.json') |
| 29 | + ) |
| 30 | + const len = Object.keys(resources).length |
| 31 | + |
| 32 | + console.log(`complex pattern on ${len} resources (JIT + AOT):`) |
| 33 | + console.log() |
| 34 | + |
| 35 | + registerMessageCompiler(compile) |
| 36 | + const precompiledResources = precompile(resources) |
| 37 | + |
| 38 | + const ctx = createCoreContext({ |
| 39 | + locale: 'en', |
| 40 | + modifiers: { |
| 41 | + caml: val => val |
| 42 | + }, |
| 43 | + messages: { |
| 44 | + en: precompiledResources |
| 45 | + } |
| 46 | + }) |
| 47 | + |
| 48 | + const i18n = createI18n({ |
| 49 | + legacy: false, |
| 50 | + locale: 'en', |
| 51 | + modifiers: { |
| 52 | + caml: val => val |
| 53 | + }, |
| 54 | + messages: { |
| 55 | + en: precompiledResources |
| 56 | + } |
| 57 | + }) |
| 58 | + |
| 59 | + new Suite('complex pattern') |
| 60 | + .add(`resolve time with core`, () => { |
| 61 | + translate(ctx, 'complex500', 2) |
| 62 | + }) |
| 63 | + .add(`resolve time on composition`, () => { |
| 64 | + clearCompileCache() |
| 65 | + i18n.global.t('complex500', 2) |
| 66 | + }) |
| 67 | + .add(`resolve time on composition with compile cache`, () => { |
| 68 | + i18n.global.t('complex500', 2) |
| 69 | + }) |
| 70 | + .on('error', event => { |
| 71 | + console.log(String(event.target)) |
| 72 | + }) |
| 73 | + .on('cycle', event => { |
| 74 | + console.log(String(event.target)) |
| 75 | + }) |
| 76 | + .run() |
| 77 | +} |
| 78 | + |
| 79 | +main().catch(err => { |
| 80 | + console.error(err) |
| 81 | + process.exit(1) |
| 82 | +}) |
0 commit comments