-
-
Notifications
You must be signed in to change notification settings - Fork 334
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: improve string join performance (#1433)
* fix: improve string join performance * fix: improve string ops performance * fix: improve string join performance * refactor
- Loading branch information
Showing
10 changed files
with
213 additions
and
150 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { createCommonJS } from 'mlly' | ||
import { baseCompile } from '@intlify/message-compiler' | ||
|
||
const { require } = createCommonJS(import.meta.url) | ||
const { Suite } = require('benchmark') | ||
|
||
async function main() { | ||
console.log(`compilation:`) | ||
console.log() | ||
|
||
new Suite('compilation') | ||
.add(`compile simple message`, () => { | ||
baseCompile(`hello world`) | ||
}) | ||
.add(`compile complex message`, () => { | ||
baseCompile(`@.caml:{'no apples'} 0 | {0} apple 0 | {n} apples 0`) | ||
}) | ||
.on('error', event => { | ||
console.log(String(event.target)) | ||
}) | ||
.on('cycle', event => { | ||
console.log(String(event.target)) | ||
}) | ||
.run() | ||
} | ||
|
||
main().catch(err => { | ||
console.error(err) | ||
process.exit(1) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,84 +1,73 @@ | ||
import { baseCompile } from '@intlify/message-compiler' | ||
import { createCommonJS } from 'mlly' | ||
import { | ||
translate, | ||
createCoreContext, | ||
clearCompileCache | ||
} from '@intlify/core-base' | ||
import { createI18n } from 'vue-i18n' | ||
import convertHrtime from 'convert-hrtime' | ||
import { resolve, dirname } from 'pathe' | ||
import { readJson } from './utils.mjs' | ||
|
||
async function run() { | ||
const { require } = createCommonJS(import.meta.url) | ||
const { Suite } = require('benchmark') | ||
|
||
async function main() { | ||
const data = await readJson(resolve(dirname('.'), './benchmark/complex.json')) | ||
const len = Object.keys(data).length | ||
|
||
console.log('complex pattern ...') | ||
|
||
console.log(`compile time: ${len} resources`) | ||
let start = convertHrtime(process.hrtime.bigint()) | ||
for (const [, source] of Object.entries(data)) { | ||
baseCompile(source) | ||
} | ||
let end = convertHrtime(process.hrtime.bigint()) | ||
console.log(`ms: ${end.milliseconds - start.milliseconds}`) | ||
|
||
console.log(`complex pattern on ${len} resources:`) | ||
console.log() | ||
|
||
console.log(`resolve time with core: ${len} resources`) | ||
const ctx = createCoreContext({ | ||
locale: 'en', | ||
modifiers: { | ||
caml: val => val | ||
}, | ||
messages: { | ||
en: data | ||
} | ||
}) | ||
start = convertHrtime(process.hrtime.bigint()) | ||
for (const [key] of Object.entries(data)) { | ||
translate(ctx, key, 2) | ||
} | ||
end = convertHrtime(process.hrtime.bigint()) | ||
console.log(`sec: ${end.seconds - start.seconds}`) | ||
console.log(`ms: ${end.milliseconds - start.milliseconds}`) | ||
let i18n | ||
|
||
clearCompileCache() | ||
console.log() | ||
new Suite('complex pattern') | ||
.add(`resolve time with core`, () => { | ||
const ctx = createCoreContext({ | ||
locale: 'en', | ||
modifiers: { | ||
caml: val => val | ||
}, | ||
messages: { | ||
en: data | ||
} | ||
}) | ||
for (const [key] of Object.entries(data)) { | ||
translate(ctx, key, 2) | ||
} | ||
}) | ||
.add(`resolve time on composition`, () => { | ||
clearCompileCache() | ||
|
||
console.log(`resolve time on composition: ${len} resources`) | ||
const i18n = createI18n({ | ||
legacy: false, | ||
locale: 'en', | ||
modifiers: { | ||
caml: val => val | ||
}, | ||
messages: { | ||
en: data | ||
} | ||
}) | ||
start = convertHrtime(process.hrtime.bigint()) | ||
for (const [key] of Object.entries(data)) { | ||
i18n.global.t(key, 2) | ||
} | ||
end = convertHrtime(process.hrtime.bigint()) | ||
console.log(`ms: ${end.milliseconds - start.milliseconds}`) | ||
i18n = createI18n({ | ||
legacy: false, | ||
locale: 'en', | ||
modifiers: { | ||
caml: val => val | ||
}, | ||
messages: { | ||
en: data | ||
} | ||
}) | ||
|
||
console.log( | ||
`resolve time on composition with compile cache: ${len} resources` | ||
) | ||
start = convertHrtime(process.hrtime.bigint()) | ||
for (const [key] of Object.entries(data)) { | ||
i18n.global.t(key, 2) | ||
} | ||
end = convertHrtime(process.hrtime.bigint()) | ||
console.log(`ms: ${end.milliseconds - start.milliseconds}`) | ||
for (const [key] of Object.entries(data)) { | ||
i18n.global.t(key, 2) | ||
} | ||
}) | ||
.add(`resolve time on composition with compile cache`, () => { | ||
for (const [key] of Object.entries(data)) { | ||
i18n.global.t(key, 2) | ||
} | ||
}) | ||
.on('error', event => { | ||
console.log(String(event.target)) | ||
}) | ||
.on('cycle', event => { | ||
console.log(String(event.target)) | ||
}) | ||
.run() | ||
} | ||
|
||
;(async () => { | ||
try { | ||
await run() | ||
} catch (e) { | ||
console.error(e) | ||
} | ||
})() | ||
main().catch(err => { | ||
console.error(err) | ||
process.exit(1) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,80 +1,66 @@ | ||
import { baseCompile } from '@intlify/message-compiler' | ||
import { createCommonJS } from 'mlly' | ||
import { | ||
translate, | ||
createCoreContext, | ||
clearCompileCache | ||
} from '@intlify/core-base' | ||
import { createI18n } from 'vue-i18n' | ||
import convertHrtime from 'convert-hrtime' | ||
import { resolve, dirname } from 'pathe' | ||
import { readJson } from './utils.mjs' | ||
|
||
async function run() { | ||
const simpleData = await readJson( | ||
resolve(dirname('.'), './benchmark/simple.json') | ||
) | ||
const len = Object.keys(simpleData).length | ||
const { require } = createCommonJS(import.meta.url) | ||
const { Suite } = require('benchmark') | ||
|
||
console.log('simple pattern ...') | ||
|
||
console.log(`compile time: ${len} resources`) | ||
let start = convertHrtime(process.hrtime.bigint()) | ||
for (const [, source] of Object.entries(simpleData)) { | ||
baseCompile(source) | ||
} | ||
let end = convertHrtime(process.hrtime.bigint()) | ||
console.log(`sec: ${end.seconds - start.seconds}`) | ||
console.log(`ms: ${end.milliseconds - start.milliseconds}`) | ||
async function main() { | ||
const data = await readJson(resolve(dirname('.'), './benchmark/simple.json')) | ||
const len = Object.keys(data).length | ||
|
||
console.log(`simple pattern on ${len} resources:`) | ||
console.log() | ||
|
||
console.log(`resolve time with core: ${len} resources`) | ||
const ctx = createCoreContext({ | ||
locale: 'en', | ||
messages: { | ||
en: simpleData | ||
} | ||
}) | ||
start = convertHrtime(process.hrtime.bigint()) | ||
for (const [key] of Object.entries(simpleData)) { | ||
translate(ctx, key) | ||
} | ||
end = convertHrtime(process.hrtime.bigint()) | ||
console.log(`ms: ${end.milliseconds - start.milliseconds}`) | ||
|
||
clearCompileCache() | ||
console.log() | ||
let i18n | ||
|
||
console.log(`resolve time on composition: ${len} resources`) | ||
const i18n = createI18n({ | ||
legacy: false, | ||
locale: 'en', | ||
messages: { | ||
en: simpleData | ||
} | ||
}) | ||
start = convertHrtime(process.hrtime.bigint()) | ||
for (const [key] of Object.entries(simpleData)) { | ||
i18n.global.t(key) | ||
} | ||
end = convertHrtime(process.hrtime.bigint()) | ||
console.log(`ms: ${end.milliseconds - start.milliseconds}`) | ||
new Suite('complex pattern') | ||
.add(`resolve time with core`, () => { | ||
const ctx = createCoreContext({ | ||
locale: 'en', | ||
messages: { | ||
en: data | ||
} | ||
}) | ||
for (const [key] of Object.entries(data)) { | ||
translate(ctx, key) | ||
} | ||
}) | ||
.add(`resolve time on composition`, () => { | ||
clearCompileCache() | ||
|
||
console.log( | ||
`resolve time on composition with compile cache: ${len} resources` | ||
) | ||
start = convertHrtime(process.hrtime.bigint()) | ||
for (const [key] of Object.entries(simpleData)) { | ||
i18n.global.t(key) | ||
} | ||
end = convertHrtime(process.hrtime.bigint()) | ||
console.log(`ms: ${end.milliseconds - start.milliseconds}`) | ||
i18n = createI18n({ | ||
legacy: false, | ||
locale: 'en', | ||
messages: { | ||
en: data | ||
} | ||
}) | ||
for (const [key] of Object.entries(data)) { | ||
i18n.global.t(key) | ||
} | ||
}) | ||
.add(`resolve time on composition with compile cache`, () => { | ||
for (const [key] of Object.entries(data)) { | ||
i18n.global.t(key) | ||
} | ||
}) | ||
.on('error', event => { | ||
console.log(String(event.target)) | ||
}) | ||
.on('cycle', event => { | ||
console.log(String(event.target)) | ||
}) | ||
.run() | ||
} | ||
|
||
;(async () => { | ||
try { | ||
await run() | ||
} catch (e) { | ||
console.error(e) | ||
} | ||
})() | ||
main().catch(err => { | ||
console.error(err) | ||
process.exit(1) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.