Skip to content

Commit

Permalink
JS call performance evaluation
Browse files Browse the repository at this point in the history
  • Loading branch information
vaind committed Dec 31, 2021
1 parent 3555002 commit 7e7f34c
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 5 deletions.
3 changes: 3 additions & 0 deletions objectbox/lib/src/web/number.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,6 @@ external num Function(num) get double;
/// ```
@JS()
external num Function(num, num) get power;

/// Multiply a value by 2 (dart code)
num double2(num v) => v * 2;
113 changes: 108 additions & 5 deletions objectbox/test/web_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,116 @@
// Since the early dev version of the JS has different APIs (e.g. Store
// constructor) than the native version, don't run on `vm`, only in the browser.
@TestOn('browser')

import 'package:test/test.dart';
import 'package:objectbox/src/web/number.dart';
import 'package:objectbox/src/web/number.dart' as webnum;

void main() {
test('test JS/TS integration', () {
// `double` is a JS function
expect(double(4), equals(8));
/// JS Code generated for the [test`] function. Note the inlined dart version.
/// function() {
/// var watch1, num1, i, watch2, num2,
/// t1 = objectbox.double.call$1(4);
/// G.expect(t1, new D._DeepMatcher(8, 100), null);
/// G.expect(8, new D._DeepMatcher(8, 100), null);
/// watch1 = new P.Stopwatch();
/// $.$get$Stopwatch__frequency();
/// watch1.start$0();
/// for (num1 = 1, i = 0; i < 10000; ++i)
/// num1 = objectbox.double.call$1(num1);
/// watch1.stop$0(0);
/// P.print("JS call takes: " + P.Duration$(watch1.get$elapsedMicroseconds(), 0).toString$0(0));
/// watch2 = new P.Stopwatch();
/// $.$get$Stopwatch__frequency();
/// watch2.start$0();
/// for (num2 = 1, i = 0; i < 10000; ++i)
/// num2 *= 2;
/// watch2.stop$0(0);
/// P.print("Dart call takes: " + P.Duration$(watch2.get$elapsedMicroseconds(), 0).toString$0(0));
/// P.print("Factor: " + H.S(watch1.get$elapsedTicks() / 10000 / (watch2.get$elapsedTicks() / 10000)));
/// G.expect(num1, new D._DeepMatcher(num2, 100), null);
/// }
test('test1', () {
num runs = 10000;
// `double` is a JS function, `double2` is implemented in dart
expect(webnum.double(4), equals(8));
expect(webnum.double2(4), equals(8));

num num1 = 1;
final watch1 = Stopwatch()..start();
for (var i = 0; i < runs; i++) {
// num1 = webnum.double(10);
num1 = webnum.double(num1);
}
watch1.stop();
print('JS call takes: ${watch1.elapsed}');

num num2 = 1;
final watch2 = Stopwatch()..start();
for (var i = 0; i < runs; i++) {
// num2 = webnum.double2(10);
num2 = webnum.double2(num2);
}
watch2.stop();
print('Dart call takes: ${watch2.elapsed}');

// Results: Dart code is about 7.6 times faster when passing the result as
// the next param (up to "Infinity"), and 15 times with a constant param.
final factor = (watch1.elapsedTicks / runs) / (watch2.elapsedTicks / runs);
print('Factor: $factor');

expect(num1, equals(num2));
});
/// JS Code generated for the [test`] function. Note the inlined dart version.
/// function() {
/// var watch1, num1, i, watch2, num2,
/// t1 = objectbox.double.call$1(4);
/// G.expect(t1, new D._DeepMatcher(8, 100), null);
/// G.expect(8, new D._DeepMatcher(8, 100), null);
/// watch1 = new P.Stopwatch();
/// $.$get$Stopwatch__frequency();
/// watch1.start$0();
/// for (num1 = 1, i = 0; i < 10000; ++i)
/// num1 = objectbox.double.call$1(num1);
/// watch1.stop$0(0);
/// P.print("JS call takes: " + P.Duration$(watch1.get$elapsedMicroseconds(), 0).toString$0(0));
/// watch2 = new P.Stopwatch();
/// $.$get$Stopwatch__frequency();
/// watch2.start$0();
/// for (num2 = 1, i = 0; i < 10000; ++i)
/// num2 *= 2;
/// watch2.stop$0(0);
/// P.print("Dart call takes: " + P.Duration$(watch2.get$elapsedMicroseconds(), 0).toString$0(0));
/// P.print("Factor: " + H.S(watch1.get$elapsedTicks() / 10000 / (watch2.get$elapsedTicks() / 10000)));
/// G.expect(num1, new D._DeepMatcher(num2, 100), null);
/// }
test('test1', () {
num runs = 10000;
// `double` is a JS function, `double2` is implemented in dart
expect(webnum.double(4), equals(8));
expect(webnum.double2(4), equals(8));

num num1 = 1;
final watch1 = Stopwatch()..start();
for (var i = 0; i < runs; i++) {
// num1 = webnum.double(10);
num1 = webnum.double(num1);
}
watch1.stop();
print('JS call takes: ${watch1.elapsed}');

num num2 = 1;
final watch2 = Stopwatch()..start();
for (var i = 0; i < runs; i++) {
// num2 = webnum.double2(10);
num2 = webnum.double2(num2);
}
watch2.stop();
print('Dart call takes: ${watch2.elapsed}');

// Results: Dart code is about 7.6 times faster when passing the result as
// the next param (up to "Infinity"), and 15 times with a constant param.
final factor = (watch1.elapsedTicks / runs) / (watch2.elapsedTicks / runs);
print('Factor: $factor');

expect(num1, equals(num2));
});
}

0 comments on commit 7e7f34c

Please sign in to comment.