Skip to content

Commit

Permalink
Update warning
Browse files Browse the repository at this point in the history
  • Loading branch information
ijjk committed May 13, 2023
1 parent 0377d59 commit 3e0a730
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions packages/next/src/shared/lib/bloom-filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,28 @@ export class BloomFilter {
bitArray: number[]

constructor(numItems: number, errorRate: number) {
if (typeof window === 'undefined' && errorRate < 0.01) {
console.warn(
`Creating filter with error rate less than 1% (0.01) can increase the size dramatically proceed with caution. Received error rate ${errorRate}`
)
}
this.numItems = numItems
this.errorRate = errorRate
this.numBits = Math.ceil(
-(numItems * Math.log(errorRate)) / (Math.log(2) * Math.log(2))
)
this.numHashes = Math.ceil((this.numBits / numItems) * Math.log(2))
this.bitArray = new Array(this.numBits).fill(0)

if (typeof window === 'undefined') {
if (errorRate < 0.01) {
const filterData = JSON.stringify(this.export())
const gzipSize = require('next/dist/compiled/gzip-size').sync(
filterData
)

if (gzipSize > 1024) {
console.warn(
`Creating filter with error rate less than 1% (0.01) can increase the size dramatically proceed with caution. Received error rate ${errorRate} resulted in size ${gzipSize} bytes (gzip)`
)
}
}
}
}

static from(items: string[], errorRate = 0.01) {
Expand Down

0 comments on commit 3e0a730

Please sign in to comment.