Filter record

Benchmark created on


Description

100 records

Setup

const data = [{"id":1,"level":"0"},
{"id":2,"level":"0"},
{"id":3,"level":"0"},
{"id":4,"level":"0"},
{"id":5,"level":"0"},
{"id":6,"level":"0"},
{"id":7,"level":"0"},
{"id":8,"level":"0"},
{"id":9,"level":"0"},
{"id":10,"level":"0"},
{"id":11,"level":"0"},
{"id":12,"level":"0"},
{"id":13,"level":"0"},
{"id":14,"level":"0"},
{"id":15,"level":"0"},
{"id":16,"level":"0"},
{"id":17,"level":"0"},
{"id":18,"level":"0"},
{"id":19,"level":"0"},
{"id":20,"level":"0"},
{"id":21,"level":"0"},
{"id":22,"level":"0"},
{"id":23,"level":"0"},
{"id":24,"level":"0"},
{"id":25,"level":"0"},
{"id":26,"level":"0"},
{"id":27,"level":"0"},
{"id":28,"level":"0"},
{"id":29,"level":"0"},
{"id":30,"level":"0"},
{"id":31,"level":"0"},
{"id":32,"level":"0"},
{"id":33,"level":"0"},
{"id":34,"level":"0"},
{"id":35,"level":"0"},
{"id":36,"level":"0"},
{"id":37,"level":"0"},
{"id":38,"level":"0"},
{"id":39,"level":"0"},
{"id":40,"level":"0"},
{"id":41,"level":"0"},
{"id":42,"level":"0"},
{"id":43,"level":"0"},
{"id":44,"level":"0"},
{"id":45,"level":"0"},
{"id":46,"level":"0"},
{"id":47,"level":"0"},
{"id":48,"level":"0"},
{"id":49,"level":"0"},
{"id":50,"level":"0"},
{"id":51,"level":"0"},
{"id":52,"level":"0"},
{"id":53,"level":"0"},
{"id":54,"level":"0"},
{"id":55,"level":"0"},
{"id":56,"level":"0"},
{"id":57,"level":"0"},
{"id":58,"level":"0"},
{"id":59,"level":"0"},
{"id":60,"level":"0"},
{"id":61,"level":"0"},
{"id":62,"level":"0"},
{"id":63,"level":"0"},
{"id":64,"level":"0"},
{"id":65,"level":"0"},
{"id":66,"level":"0"},
{"id":67,"level":"0"},
{"id":68,"level":"0"},
{"id":69,"level":"0"},
{"id":70,"level":"0"},
{"id":71,"level":"0"},
{"id":72,"level":"0"},
{"id":73,"level":"0"},
{"id":74,"level":"0"},
{"id":75,"level":"0"},
{"id":76,"level":"0"},
{"id":77,"level":"0"},
{"id":78,"level":"0"},
{"id":79,"level":"0"},
{"id":80,"level":"0"},
{"id":81,"level":"0"},
{"id":82,"level":"0"},
{"id":83,"level":"0"},
{"id":84,"level":"0"},
{"id":85,"level":"0"},
{"id":86,"level":"0"},
{"id":87,"level":"0"},
{"id":88,"level":"0"},
{"id":89,"level":"0"},
{"id":90,"level":"0"},
{"id":91,"level":"0"},
{"id":92,"level":"0"},
{"id":93,"level":"0"},
{"id":94,"level":"0"},
{"id":95,"level":"0"},
{"id":96,"level":"0"},
{"id":97,"level":"0"},
{"id":98,"level":"0"},
{"id":99,"level":"1"},
{"id":100,"level":"1"}];

Test runner

Ready to run.

Testing in
TestOps/sec
Using filter
try{
	const rdata = 	data.filter((r) => r.level === '1');
if(rdata.length>1)
throw new Error('error');
}
catch(e){
	console.log(e);
}
ready
Using for loop
try{
	let selectedRecords = data;
	if (selectedRecords.length > 0) {
					let parentRec = null;
					let multiParent = false;
					for (let i = 0; i < selectedRecords.length; i++) {
						let record = selectedRecords[i];
						if (record.childLevel === '1') {
							if (parentRec) {
								multiParent = true;
								break;
							}
							parentRec = record;
						}
					}
					if (!parentRec || multiParent) {
						throw new Error('error');
					}
}
}
catch(e){
	console.log(e);
}
ready
using forEach 1
try{
	let parentRec = null;
	data.forEach((r)=>{
		if (r.childLevel === '1' && parentRec == null)  parentRec = r;
		else if(r.childLevel === '1') throw new Error('error');
	});
}
catch(e){
	console.log(e);
}
ready
using forEach 2
try{
	let parentRec = null;
	data.forEach((r)=>{
		if (r.childLevel === '1'){
			if(parentRec == null)
			parentRec = r;
			else
			throw new Error('error');
		}
		
	});
}
catch(e){
	console.log(e);
}
ready

Revisions

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