Created
May 27, 2022 20:26
-
-
Save srikanthkyatham/8d90f41fc4575e1d946399c2cb200e6f to your computer and use it in GitHub Desktop.
array object filter
This file contains 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
function randomIntFromInterval(min, max) { // min and max included | |
return Math.floor(Math.random() * (max - min + 1) + min) | |
} | |
function makeid(length) { | |
var result = ''; | |
var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; | |
var charactersLength = characters.length; | |
for ( var i = 0; i < length; i++ ) { | |
result += characters.charAt(Math.floor(Math.random() * | |
charactersLength)); | |
} | |
return result; | |
} | |
const createObject = () => { | |
return { | |
a: makeid(randomIntFromInterval(0,20)), | |
b: makeid(randomIntFromInterval(0, 20)), | |
c: makeid(randomIntFromInterval(0, 20)), | |
d: makeid(randomIntFromInterval(0, 20)), | |
e: makeid(randomIntFromInterval(0, 20)), | |
f: makeid(randomIntFromInterval(0, 20)), | |
} | |
} | |
let array = [] | |
for (let i = 0; i < 15000; i++) { | |
array.push(createObject()); | |
} | |
const needle = 'a'; | |
console.time("find needle") | |
const filteredArray = array.filter((item) => { | |
return item.a.startsWith(needle) || item.b.startsWith(needle) || item.c.startsWith(needle) | |
}) | |
console.timeEnd("find needle") | |
console.log({filteredArray}) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment