Created
February 4, 2023 19:32
-
-
Save H4ad/2600b4b8067e0a52b642a733c357c125 to your computer and use it in GitHub Desktop.
Node Postgres Array Performance Analysis
This file contains hidden or 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
var Benchmark = require('benchmark'); | |
var array = require('postgres-array'); | |
var fasterArray = require('./faster-postgres-array'); | |
function parseBool (value) { | |
if (value === null) return value | |
return value === 'TRUE' || | |
value === 't' || | |
value === 'true' || | |
value === 'y' || | |
value === 'yes' || | |
value === 'on' || | |
value === '1'; | |
} | |
function parseBoolArray(value) { | |
if (!value) return null; | |
return array.parse(value, parseBool); | |
} | |
function parseBoolArrayV2(value) { | |
if (!value) return null; | |
return fasterArray.parse(value, parseBool); | |
} | |
var suite = new Benchmark.Suite(); | |
var caseOne = '{true}'; | |
var caseTwo = '{true,false}'; | |
var caseThree = '{true,false,true}'; | |
suite.add(`parseBoolArray('${caseOne}')`, async function () { | |
const r = parseBoolArray(caseOne); | |
}); | |
suite.add(`parseBoolArray('${caseTwo}')`, async function () { | |
const r = parseBoolArray(caseTwo); | |
}); | |
suite.add(`parseBoolArray('${caseThree}')`, async function () { | |
const r = parseBoolArray(caseThree); | |
}); | |
suite.add(`parseBoolArrayV2('${caseOne}')`, async function () { | |
const r = parseBoolArrayV2(caseOne); | |
}); | |
suite.add(`parseBoolArrayV2('${caseTwo}')`, async function () { | |
const r = parseBoolArrayV2(caseTwo); | |
}); | |
suite.add(`parseBoolArrayV2('${caseThree}')`, async function () { | |
const r = parseBoolArrayV2(caseThree); | |
}); | |
suite | |
// add listeners | |
.on('cycle', function (event) { | |
console.log(String(event.target)); | |
}) | |
.on('complete', function () { | |
console.log('Fastest is ' + this.filter('fastest').map('name')); | |
}) | |
.run({ | |
async: true, | |
}); |
This file contains hidden or 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
'use strict' | |
exports.parse = function (source, transform) { | |
return parsePostgresArray(source, transform); | |
} | |
function parsePostgresArray(source, transform, nested = false) { | |
var character = '' | |
var quote = false; | |
var position = 0; | |
var dimension = 0; | |
var entries = []; | |
var recorded = ''; | |
var newEntry = function (includeEmpty) { | |
var entry = recorded; | |
if (entry.length > 0 || includeEmpty) { | |
if (entry === 'NULL' && !includeEmpty) { | |
entry = null | |
} | |
if (entry !== null && transform) { | |
entry = transform(entry) | |
} | |
entries.push(entry) | |
recorded = ''; | |
} | |
} | |
if (source[0] === '[') { | |
while (position < source.length) { | |
var char = source[position++]; | |
if (char === '=') | |
break | |
} | |
} | |
while(position < source.length) { | |
var escaped = false; | |
var character = source[position++]; | |
if (character === '\\') { | |
character = source[position++]; | |
escaped = true; | |
} | |
if (character === '{' && !quote) { | |
dimension++; | |
if (dimension > 1) { | |
var parser = parsePostgresArray(source.substr(position - 1), transform, true); | |
entries.push(parser.entries) | |
position += parser.position - 2 | |
} | |
} else if (character === '}' && !quote) { | |
dimension--; | |
if (!dimension) { | |
newEntry(); | |
if (nested) { | |
return { | |
entries, | |
position, | |
}; | |
} | |
} | |
} else if (character === '"' && !escaped) { | |
if (quote) { | |
newEntry(true); | |
} | |
quote = !quote; | |
} else if (character === ',' && !quote) { | |
newEntry(); | |
} else { | |
recorded += character; | |
} | |
} | |
if (dimension !== 0) { | |
throw new Error('array dimension not balanced') | |
} | |
return entries; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment