Some performance optimizations

This commit is contained in:
User user 2021-05-18 14:10:59 +02:00
parent 8e183ac1a5
commit 7ca0c4fd72
6 changed files with 5917 additions and 18 deletions

54
benchmark.js Normal file
View File

@ -0,0 +1,54 @@
const { LoggingBase } = require("./out/index.js");
let results = {};
function benchmark(name, count, runner) {
console.profile(name);
const start = process.hrtime.bigint()
runner(count);
const diffNS = process.hrtime.bigint() - start;
const diffMS = Number(diffNS / 1000n / 1000n);
console.profileEnd(name)
results[name]= {
count,
time: diffMS,
timePerI: (diffMS / count).toFixed(4)
}
}
benchmark("simple", 10000000, (cnt) => {
const l = new LoggingBase({
console: false
});
for (let i = 0; i < cnt; i++) {
l.log("simple log")
}
})
benchmark("complex", 1000000, (cnt) => {
const l = new LoggingBase({
console: false
});
for (let i = 0; i < cnt; i++) {
l.log("complex log", {
a: 1,
b: {
c:"test"
}
})
}
})
benchmark("very long", 10000000, (cnt) => {
const l = new LoggingBase({
console: false
});
const longText = "complex log".repeat(100);
for (let i = 0; i < cnt; i++) {
l.log(longText)
}
})
console.table(results)

View File

@ -1,6 +1,6 @@
{ {
"name": "logging", "name": "logging",
"version": "3.1.0", "version": "3.1.1",
"description": "", "description": "",
"author": "Fabian Stamm <dev@fabianstamm.de>", "author": "Fabian Stamm <dev@fabianstamm.de>",
"contributors": [], "contributors": [],

5839
package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

View File

@ -1,6 +1,6 @@
{ {
"name": "@hibas123/logging", "name": "@hibas123/logging",
"version": "3.1.0", "version": "3.1.1",
"description": "", "description": "",
"main": "out/index.js", "main": "out/index.js",
"types": "out/index.d.ts", "types": "out/index.d.ts",
@ -9,7 +9,9 @@
"prepublishOnly": "npm run build", "prepublishOnly": "npm run build",
"build": "tsc && tsc -p tsconfig.esm.json", "build": "tsc && tsc -p tsconfig.esm.json",
"test": "tsc && node out/test.js", "test": "tsc && node out/test.js",
"postpublish": "denreg publish" "postpublish": "denreg publish",
"bench": "tsc && node benchmark.js",
"prof": "tsc && ndb --prof benchmark.js"
}, },
"repository": { "repository": {
"type": "git", "type": "git",
@ -26,6 +28,7 @@
], ],
"devDependencies": { "devDependencies": {
"concurrently": "^6.0.2", "concurrently": "^6.0.2",
"ndb": "^1.1.5",
"nodemon": "^2.0.7", "nodemon": "^2.0.7",
"ts-node": "^9.1.1", "ts-node": "^9.1.1",
"typescript": "^4.2.4" "typescript": "^4.2.4"

View File

@ -243,7 +243,8 @@ export class LoggingBase extends LoggingInterface {
} }
const date = new Date(); const date = new Date();
const date_str = date.toISOString().replace(/T/, " ").replace(/\..+/, ""); const isoStr = date.toISOString();
const date_str = isoStr.substring(0, 10) + " " + isoStr.substring(11, 19);
let file: string | undefined = undefined; let file: string | undefined = undefined;
if (this.#resolve_filename) { if (this.#resolve_filename) {
@ -321,10 +322,12 @@ export class LoggingBase extends LoggingInterface {
let formattedMessage: Formatted<string>[] = [...linePrefix]; let formattedMessage: Formatted<string>[] = [...linePrefix];
message.forEach((msg, idx) => { message.forEach((msg, idx) => {
let format = new Formatted(); let format: Formatted;
if (msg instanceof Formatted) { if (msg instanceof Formatted) {
format = msg; format = msg;
msg = msg.content; msg = msg.content;
} else {
format = new Formatted();
} }
if (typeof msg !== "string") { if (typeof msg !== "string") {
@ -335,9 +338,8 @@ export class LoggingBase extends LoggingInterface {
}) as string; }) as string;
} }
removeColors(msg) // removeColors(msg) // Remove colors is uncommented for now, since there are no real benefits of having it and it reduces performance
.split("\n") msg.split("\n").forEach((text, index, { length }) => {
.forEach((text, index, { length }) => {
if (index != length - 1) { if (index != length - 1) {
formattedMessage.push( formattedMessage.push(
new Formatted(text + "\n", format), new Formatted(text + "\n", format),
@ -462,6 +464,7 @@ function getCallerFromExisting(err: Error): {
export function removeColors(text: string) { export function removeColors(text: string) {
text = text.replace( text = text.replace(
// Putting regex here directly instead of externally actually improves performance. The cause of that is not clear for now.
/[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g, /[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g,
"" ""
); );

View File

@ -203,7 +203,7 @@ export class DefaultFormatConfig implements FormatConfig {
date = new Formatted()._color(IColors.NONE); date = new Formatted()._color(IColors.NONE);
file = new Formatted()._color(IColors.NONE); file = new Formatted()._color(IColors.NONE);
names = new Formatted()._bold()._color(IColors.GREEN); names = new Formatted()._bold()._color(IColors.CYAN);
names_delimiter = new Formatted(" -> ")._bold(); names_delimiter = new Formatted(" -> ")._bold();
} }