Skip to content

Instantly share code, notes, and snippets.

@jairoFernandez
Created December 15, 2020 18:13
Show Gist options
  • Save jairoFernandez/04c677b7128c52d7ae68fce4f4b41a75 to your computer and use it in GitHub Desktop.
Save jairoFernandez/04c677b7128c52d7ae68fce4f4b41a75 to your computer and use it in GitHub Desktop.
Full example
import 'dart:math';
var rng = new Random();
void main(List<String> args) {
final quantity = 1000;
final stopwatch = Stopwatch()..start();
final products = createProducts(quantity);
final users = createUsers(quantity);
final usersIndexed = indexerListUser(users);
goodCombinator(products, usersIndexed);
// badCombinatorFunction(products, users);
print(
'Process for $quantity, executed in ${stopwatch.elapsed.inMilliseconds} milliseconds');
stopwatch.stop();
}
goodCombinator(List<dynamic> products, List<dynamic> users) {
final data = [];
for (var i = 0; i < products.length; i++) {
final user = users[products[i]['user']];
data.add({
"id": products[i]['id'],
"name": products[i]['name'],
"user": user,
});
}
return data;
}
indexerListUser(List<dynamic> users) {
final data = [];
for (var i = 0; i < users.length; i++) {
data.add({users[i]['id']: users[i]});
}
return data;
}
badCombinatorFunction(List<dynamic> products, List<dynamic> users) {
final data = [];
for (var i = 0; i < products.length; i++) {
final user = users.firstWhere((u) => u['id'] == products[i]['user']);
data.add({
"id": products[i]['id'],
"name": products[i]['name'],
"user": user,
});
}
return data;
}
createProducts(int quantity) {
final data = [];
for (var i = 0; i < quantity; i++) {
data.add({"id": i, "name": "name $i", "user": rng.nextInt(quantity - 1)});
}
return data;
}
createUsers(int quantity) {
final data = [];
for (var i = 0; i < quantity; i++) {
data.add({"id": i, "user": "User $i"});
}
return data;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment