L
Size: a a a
L
DE
L
DE
DE
L
L
DE
function getRandomLetter() {
return String.fromCharCode((Math.random() * 25 + 97) ^ 0);
}
let i = 0
const obj = {}
let tmp = obj
const I = 25
function addProps (index = 0, o = {}) {
if (index < I) {
const l1 = getRandomLetter()
const l2 = getRandomLetter()
o[l1] = {}
o[l1] = addProps(index + 1, o[l1])
o[l2] = {}
o[l2] = addProps(index + 1, o[l2])
return o
}
o['a'] = true
o['b'] = true
return o
}
console.time(I)
tmp = addProps(0, tmp)
console.timeEnd(I)
function makeString(o, p = '') {
if (typeof o === 'boolean') {
return p
}
const key = Object.keys(o)[Math.random() ^ 0]
return makeString(o[key], p + key)
}
makeString(tmp)
S
function getRandomLetter() {
return String.fromCharCode((Math.random() * 25 + 97) ^ 0);
}
let i = 0
const obj = {}
let tmp = obj
const I = 25
function addProps (index = 0, o = {}) {
if (index < I) {
const l1 = getRandomLetter()
const l2 = getRandomLetter()
o[l1] = {}
o[l1] = addProps(index + 1, o[l1])
o[l2] = {}
o[l2] = addProps(index + 1, o[l2])
return o
}
o['a'] = true
o['b'] = true
return o
}
console.time(I)
tmp = addProps(0, tmp)
console.timeEnd(I)
function makeString(o, p = '') {
if (typeof o === 'boolean') {
return p
}
const key = Object.keys(o)[Math.random() ^ 0]
return makeString(o[key], p + key)
}
makeString(tmp)
DE
DE
DE
L
class Node {
constructor(char, parent, inc = true) {
this.parent = parent
this.char = char
this.children = []
this.leaf = false
this.numLeafs = 1
if ( inc )
while(parent) {
parent.numLeafs++
parent = parent.parent
}
}
}
class Tree {
constructor() {
this.root = new Node("", null, false)
this.root.numLeafs = 0
}
add(word, node = this.root) {
for(let i = 0; i < word.length; i++) {
const char = word[i]
const childNode = node.children.find(n => n.char[0] === char)
if ( childNode ) {
if ( childNode.char.length === 1 ) {
node = childNode
continue
} else {
const word1 = childNode.char.slice(1)
childNode.char = childNode.char[0]
childNode.children.push( new Node(word1, childNode, false) )
this.add(word.slice(i + 1), childNode)
break
}
} else {
node.children.push( new Node(word.slice(i), node, true) )
break
}
}
}
getAllNumNodes(children = this.root.children) {
return children.reduce((s, node) => s + 1 + this.getAllNumNodes(node.children), 0)
}
likeStringCount(pattern) {
let node = this.root
next:
while(1) {
if ( !pattern.length )
return node.numLeafs
let children = node.children
for(const childNode of children) {
if ( childNode.children.length ) {
if ( childNode.char === pattern[0] ) {
pattern = pattern.slice(1)
node = childNode
continue next;
}
} else {
if ( childNode.char.indexOf(pattern) === 0 ) {
pattern = ""
node = childNode
continue next;
}
}
}
return 0
}
}
}
const t = new Tree
function getStringRand(len = 100) {
let s = ""
while(len--)
s += String.fromCharCode(97 + (Math.random()*25|0))
return s
}
console.time('init')
for(let i = 0; i < 1e6; i++) {
const s = getStringRand()
t.add( s )
}
console.timeEnd('init')
function like(pattern) {
let tn = Date.now()
const count = t.likeStringCount(pattern)
tn = Date.now() - tn
console.log(`(time: ${ tn }msec) Like '${ pattern }.*' count: ` + count)
}
like('a')
like('ab')
like('abc')
like('abce')
L
DE
function getRandomLetter() {
return String.fromCharCode((Math.random() * 25 + 97) ^ 0).repeat(4);
}
let tmp = {}
const I = 25
function addProps (index = 0, o = {}) {
if (index < I) {
const l1 = getRandomLetter()
const l2 = getRandomLetter()
o[l1] = {}
o[l1] = addProps(index + 1, o[l1])
o[l2] = {}
o[l2] = addProps(index + 1, o[l2])
return o
}
return true
}
console.time(I)
tmp = addProps(0, tmp)
console.timeEnd(I)
function makeString(o, p = '') {
if (typeof o === 'boolean') {
return p
}
const key = Object.keys(o)[Math.random() ^ 0]
return makeString(o[key], p + key)
}
console.time('toStr')
const res = makeString(tmp)
console.timeEnd('toStr')
console.log(res)
S
function getRandomLetter() {
return String.fromCharCode((Math.random() * 25 + 97) ^ 0).repeat(4);
}
let tmp = {}
const I = 25
function addProps (index = 0, o = {}) {
if (index < I) {
const l1 = getRandomLetter()
const l2 = getRandomLetter()
o[l1] = {}
o[l1] = addProps(index + 1, o[l1])
o[l2] = {}
o[l2] = addProps(index + 1, o[l2])
return o
}
return true
}
console.time(I)
tmp = addProps(0, tmp)
console.timeEnd(I)
function makeString(o, p = '') {
if (typeof o === 'boolean') {
return p
}
const key = Object.keys(o)[Math.random() ^ 0]
return makeString(o[key], p + key)
}
console.time('toStr')
const res = makeString(tmp)
console.timeEnd('toStr')
console.log(res)
L
function getRandomLetter() {
return String.fromCharCode((Math.random() * 25 + 97) ^ 0).repeat(4);
}
let tmp = {}
const I = 25
function addProps (index = 0, o = {}) {
if (index < I) {
const l1 = getRandomLetter()
const l2 = getRandomLetter()
o[l1] = {}
o[l1] = addProps(index + 1, o[l1])
o[l2] = {}
o[l2] = addProps(index + 1, o[l2])
return o
}
return true
}
console.time(I)
tmp = addProps(0, tmp)
console.timeEnd(I)
function makeString(o, p = '') {
if (typeof o === 'boolean') {
return p
}
const key = Object.keys(o)[Math.random() ^ 0]
return makeString(o[key], p + key)
}
console.time('toStr')
const res = makeString(tmp)
console.timeEnd('toStr')
console.log(res)
DE
DE
function getRandomLetter() {
return String.fromCharCode((Math.random() * 25 + 97) ^ 0).repeat(4);
}
let tmp = {}
const I = 25
function addProps (index = 0, o = {}) {
if (index < I) {
const l1 = getRandomLetter()
const l2 = getRandomLetter()
o[l1] = {}
o[l1] = addProps(index + 1, o[l1])
o[l2] = {}
o[l2] = addProps(index + 1, o[l2])
return o
}
return true
}
console.time(I)
tmp = addProps(0, tmp)
console.timeEnd(I)
function makeString(o, p = '') {
if (typeof o === 'boolean') {
return p
}
const key = Object.keys(o)[Math.round(Math.random())]
return makeString(o[key], p + key)
}
console.time('toStr')
const res = makeString(tmp)
console.timeEnd('toStr')
console.log(res)
console.log(res.length)
S
function getRandomLetter() {
return String.fromCharCode((Math.random() * 25 + 97) ^ 0).repeat(4);
}
let tmp = {}
const I = 25
function addProps (index = 0, o = {}) {
if (index < I) {
const l1 = getRandomLetter()
const l2 = getRandomLetter()
o[l1] = {}
o[l1] = addProps(index + 1, o[l1])
o[l2] = {}
o[l2] = addProps(index + 1, o[l2])
return o
}
return true
}
console.time(I)
tmp = addProps(0, tmp)
console.timeEnd(I)
function makeString(o, p = '') {
if (typeof o === 'boolean') {
return p
}
const key = Object.keys(o)[Math.round(Math.random())]
return makeString(o[key], p + key)
}
console.time('toStr')
const res = makeString(tmp)
console.timeEnd('toStr')
console.log(res)
console.log(res.length)