Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions benchmark/enum_benchmark.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import 'package:dartobjectutils/dartobjectutils.dart';
import 'dart:core';

class MyEnum {
final String name;
const MyEnum(this.name);
@override
String toString() => "MyEnum.$name";
}

void main() {
// Setup
// Make these large enough to show a difference.
// 1000 enum values
// 10000 input elements
final int enumSize = 1000;
final int inputSize = 10000;

final values = List.generate(enumSize, (i) => MyEnum('val$i'));
// Create inputs that are scattered across the enum values
final inputList = List.generate(inputSize, (i) => 'val${i % enumSize}');

final map = {'data': inputList};

print('Running benchmark with enumSize=$enumSize, inputSize=$inputSize');

// Warmup
print('Warming up...');
for (int i = 0; i < 5; i++) {
getEnumArrayPropOrThrow(map, 'data', values);
}

// Measure
print('Measuring...');
final stopwatch = Stopwatch()..start();
final iterations = 20;
for (int i = 0; i < iterations; i++) {
getEnumArrayPropOrThrow(map, 'data', values);
}
stopwatch.stop();

print('Total time: ${stopwatch.elapsedMilliseconds} ms');
print('Average time: ${stopwatch.elapsedMilliseconds / iterations} ms');
}
83 changes: 43 additions & 40 deletions example/dartobjectutils_example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ class Address {
final String zip;

Address(Map<String, dynamic> json)
: street = getStringPropOrThrow(json, 'street'),
city = getStringPropOrThrow(json, 'city'),
// Use default value for optional fields
zip = getStringPropOrDefault(json, 'zip', '00000');
: street = getStringPropOrThrow(json, 'street'),
city = getStringPropOrThrow(json, 'city'),
// Use default value for optional fields
zip = getStringPropOrDefault(json, 'zip', '00000');

@override
String toString() => '$street, $city $zip';
Expand All @@ -27,28 +27,31 @@ class User {
final Map<String, int> scores;

User(Map<String, dynamic> json)
: name = getStringPropOrThrow(json, 'name'),
// Automatically converts compatible types (e.g. String "30" -> int 30)
age = getNumberPropOrDefault(json, 'age', 0).toInt(),
isActive = getBooleanPropOrDefault(json, 'isActive', false),
// Parse nested object
address = getObjectFunctionPropOrThrow(
json,
'address',
(j) => Address(j),
),
// Parse array of primitives
tags = getStringArrayPropOrDefault(json, 'tags', <String>[]),
// Parse array of objects
prevAddresses = getObjectArrayFunctionPropOrDefault(
json,
'prevAddresses',
(j) => Address(j),
<Address>[],
)!, // ! is safe because we provide a non-null default value
// Explicitly specify generics for type safety when extracting Maps
scores = getMapPropOrDefault<String, int, Map<String, int>>(
json, 'scores', <String, int>{});
: name = getStringPropOrThrow(json, 'name'),
// Automatically converts compatible types (e.g. String "30" -> int 30)
age = getNumberPropOrDefault(json, 'age', 0).toInt(),
isActive = getBooleanPropOrDefault(json, 'isActive', false),
// Parse nested object
address = getObjectFunctionPropOrThrow(
json,
'address',
(j) => Address(j),
),
// Parse array of primitives
tags = getStringArrayPropOrDefault(json, 'tags', <String>[]),
// Parse array of objects
prevAddresses = getObjectArrayFunctionPropOrDefault(
json,
'prevAddresses',
(j) => Address(j),
<Address>[],
)!, // ! is safe because we provide a non-null default value
// Explicitly specify generics for type safety when extracting Maps
scores = getMapPropOrDefault<String, int, Map<String, int>>(
json,
'scores',
<String, int>{},
);

@override
String toString() {
Expand All @@ -66,17 +69,13 @@ void main() {
'name': 'Alice',
'age': 30,
'isActive': true,
'address': {
'street': '123 Main St',
'city': 'Wonderland',
'zip': '12345',
},
'address': {'street': '123 Main St', 'city': 'Wonderland', 'zip': '12345'},
'tags': ['admin', 'editor'],
'prevAddresses': [
{'street': '456 Old St', 'city': 'OldTown', 'zip': '67890'},
{'street': '789 Unknown St', 'city': 'Nowhere'}
{'street': '789 Unknown St', 'city': 'Nowhere'},
],
'scores': {'math': 95, 'science': 88}
'scores': {'math': 95, 'science': 88},
};

print('\n1. Parsing Valid User:');
Expand All @@ -90,10 +89,7 @@ void main() {
// Case 2: Missing optional fields (using defaults)
final minimalJson = {
'name': 'Bob',
'address': {
'street': '1st Ave',
'city': 'Metropolis',
},
'address': {'street': '1st Ave', 'city': 'Metropolis'},
// 'age' missing -> defaults to 0
// 'isActive' missing -> defaults to false
// 'tags' missing -> defaults to []
Expand All @@ -111,7 +107,8 @@ void main() {
// Case 3: Invalid data (throwing error)
final invalidJson = {
'name': 'Charlie',
'age': 'not a number', // getNumberPropOrDefault handles conversion failure -> defaults to 0
'age':
'not a number', // getNumberPropOrDefault handles conversion failure -> defaults to 0
'address': 'Not a map', // getObjectFunctionPropOrThrow throws exception
};

Expand Down Expand Up @@ -145,11 +142,17 @@ void main() {

// Demonstration of manual extraction before creating the object
print('Decoded name: ${getStringPropOrThrow(decoded, "name")}');
print('Decoded age: ${getNumberPropOrThrow(decoded, "age")}'); // Auto converts string "45" to num
print(
'Decoded age: ${getNumberPropOrThrow(decoded, "age")}',
); // Auto converts string "45" to num

// Custom handling for boolean if API returns 0/1 instead of true/false
final isActive = getBooleanFunctionPropOrDefault(
decoded, 'isActive', (v) => v == 1 || v == true, false);
decoded,
'isActive',
(v) => v == 1 || v == true,
false,
);
print('Decoded isActive (custom logic): $isActive');

// Create User object
Expand Down
Loading