Test

Benchmark created on


Setup

let hit =  {
    id: '05211755-30',
    score: 0.5,
    document: { text: 'hello world', embedding: [ 1, 2, 3, 4, 4 ], number: 2 }
  }
let searchResult = {
  count: 20,
  elapsed: { raw: 979855, formatted: '979μs' },
  hits: [{
    id: '05211755-29',
    score: 0.5,
    document: { text: 'hello world', embedding: [ 0, 41, 10, 39, 12 ], number: 1 }
  },
 ]
};

let vectorProperties = []

for (let i = 0; i< 1000;i++) {
	vectorProperties.push('embedding')
}

for (let i = 0; i< 1000;i++) {
	searchResult.hits.push(hit)
}

Test runner

Ready to run.

Testing in
TestOps/sec
old
  searchResult.hits = searchResult.hits.map((result) => ({
    ...result,
    document: {
      ...result.document,
      // Remove embeddings from the result
      ...vectorProperties.reduce((acc, prop) => {
        const path = prop.split('.')
        const lastKey = path.pop()
        let obj = acc
        for (const key of path) {
          obj[key] = obj[key] ?? {}
          obj = obj[key]
        }
        obj[lastKey] = null
        return acc
      }, result.document)
    }
  }))
ready
new
  const hitsLen = searchResult.hits.length
  const vectorPropertiesLen = vectorProperties.length

  for (let i = 0;i < hitsLen; i++) {
    const doc = searchResult.hits[i].document
  
    for (let j = 0;j < vectorPropertiesLen; j++) {
      let obj = doc
      const split = vectorProperties[j].split('.')
      const splitLen = split.length

      for (let k = 0; k < splitLen - 1;k++) {
        obj[split[k]] = obj[split[k]] ?? {}
        obj = obj[split[k]]
      }

      obj[split[splitLen-1]] = null
    }
  }
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.