Last active
August 29, 2015 14:00
-
-
Save shawndrape/11200712 to your computer and use it in GitHub Desktop.
I created this quick test to see if my intended method of expanding json_object would impact run time too much.
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
import 'dart:mirrors'; | |
import 'package:json_object/json_object.dart'; | |
import 'package:unittest/unittest.dart'; | |
import 'package:unittest/vm_config.dart'; | |
class ControlClass{ | |
String A; | |
int B; | |
double C; | |
} | |
class TestClass{ | |
String A; | |
int B; | |
Map<String, Object> _backupData = {}; | |
TestClass.buildFromMap(Map map){ | |
_buildFromMap(map); | |
} | |
_buildFromMap(map){ | |
InstanceMirror im = reflect(this); | |
ClassMirror cm = im.type; | |
Map objData = cm.instanceMembers; | |
map.forEach((key, value){ | |
Symbol keySymbol = new Symbol(key + "="); //adding "=" finds setters | |
if (objData.containsKey(keySymbol)){ | |
im.setField(new Symbol(key), value); | |
} | |
else{ | |
_backupData[key] = value; | |
} | |
}); | |
} | |
noSuchMethod(Invocation invo){ | |
if (invo.isAccessor && invo.isGetter){ | |
String memberName = MirrorSystem.getName(invo.memberName); | |
return _backupData[memberName]; | |
} else { | |
super.noSuchMethod(invo); | |
} | |
} | |
} | |
main(){ | |
useVMConfiguration(); | |
TestClass testObject; | |
JsonObject jsonObject; | |
ControlClass controlObject; | |
var timer = new Stopwatch(); | |
var fieldMap = {"A":"Words","B":14,"C":4.59}; | |
setUp((){ | |
timer.start(); | |
testObject = new TestClass.buildFromMap(fieldMap); | |
print("TestClass Created in ${timer.elapsedMilliseconds}ms"); | |
timer.reset(); | |
controlObject = new ControlClass() | |
..A = "Words" | |
..B = 14 | |
..C = 4.59; | |
print("ControlClass Created in ${timer.elapsedMilliseconds}ms"); | |
timer.reset(); | |
jsonObject = new JsonObject.fromMap(fieldMap); | |
print("JsonObject Created in ${timer.elapsedMilliseconds}ms"); | |
timer.stop(); | |
}); | |
test('fieldAccess',(){ | |
timer.start(); | |
expect(testObject.A, equals("Words")); | |
expect(testObject.B, equals(14)); | |
expect(testObject.C, equals(4.59)); | |
print("testObject accessed in ${timer.elapsedMilliseconds}ms"); | |
timer.reset(); | |
expect(controlObject.A, equals("Words")); | |
expect(controlObject.B, equals(14)); | |
expect(controlObject.C, equals(4.59)); | |
print("controlObject accessed in ${timer.elapsedMilliseconds}ms"); | |
timer.reset(); | |
expect(jsonObject.A, equals("Words")); | |
expect(jsonObject.B, equals(14)); | |
expect(jsonObject.C, equals(4.59)); | |
print("jsonObject accessed in ${timer.elapsedMilliseconds}ms"); | |
timer.stop(); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Creation times
TestClass Created in 28ms
ControlClass Created in 0ms
JsonObject Created in 5ms
Access times
testObject accessed in 23ms
controlObject accessed in 0ms
jsonObject accessed in 1ms