For doing that I have to go deeper in to the language. I started reading "Dart in action" (I completely recommend this book for learning in Dart).
While I was reading the book, I read a chapter about the dart2js function. This function converts dart source code to JavaScript code. The author speaks about the hard work made by google to try to do the conversion between Dart and JavaScript as efficient as they can. After reading this I thought about writing an article about this topic.
What I am going to prove is the performance of Dart, JavaScript converted from Dart and JavaScript doing a easy task: Calculate the first 250.000 prime numbers.
There are thousand different forms to calculate if a number is prime or not. I am not trying to prove what is the most efficient one, what I am trying to prove what is the fastest combo (browser + "language") to calculate them using the same algorithm.
These are the source codes I have used.
performace_js.dart
import 'dart:html';
import 'dart:core';
import 'dart:async';
void main() {
InputElement start = querySelector("#start");
start.onClick.listen(prime_number);
}
void prime_number(Event e){
for(int w=0;w<10;w++){
int numberOfPrimes =0;
var start = new DateTime.now();
for (int i=0;i<=250000;i++){
if (isPrime(i)) numberOfPrimes++;
}
var end = new DateTime.now();
var timeElasped= end.difference(start);
print(timeElasped);
}
}
bool isPrime(int number){
for(int i=2;i<number;i++) {
if(number%i==0)
return false;
}
return true;
}
performace_js.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Performace JS</title>
<link rel="stylesheet" href="performace_js.css">
</head>
<body>
<h1>Performace JS</h1>
<div id="sample_container_id">
<input type="button" id="start" value="Click_me" />
</div>
<script type="application/dart" src="performace_js.dart"></script>
<script src="packages/browser/dart.js"></script>
</body>
</html>
performace_js2.html (HTML+JAVASCRIPT)
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Performace JS</title>
<link rel="stylesheet" href="performace_js.css">
<script>
function isPrime(num) {
if(num < 2) return false;
for (var i = 2; i < num; i++) {
if(num%i==0)
return false;
}
return true;
}
function prime_number(){
for (var w=0;w<10;w++){
var numberOfPrimes=0;
var start = new Date().getTime();
for (var j=0;j<=250000;j++){
if(isPrime(j)==true){
numberOfPrimes++;
}
}
var end = new Date().getTime();
var time = end - start;
console.log('Execution time: ' + time + ' aaaaa '+ numberOfPrimes);
}
}
</script>
</head>
</head>
<body>
<h1>Performace JS</h1>
<div id="sample_container_id">
<input type="button" id="start" value="Click_me" onclick="prime_number()" />
</div>
</body>
</html>
With this code we get an easy web page :
After testing the Dart, JavaScript converted from Dart and the JavaScript code the results are these:
Run/Browser | Dartium | Chrome dart2js | Firefox dart2js | IE dart2js | Chromium JS | Chrome JS | Firefox JS | IE JS |
1 | 10,90 | 13,67 | 22,94 | 34,78 | 10,81 | 10,81 | 12,34 | 20,74 |
2 | 10,86 | 13,11 | 22,90 | 34,88 | 10,86 | 10,78 | 11,57 | 20,57 |
3 | 10,89 | 13,10 | 22,89 | 34,65 | 10,79 | 10,76 | 11,57 | 20,58 |
4 | 10,90 | 13,09 | 22,89 | 34,70 | 10,77 | 10,84 | 11,58 | 20,56 |
5 | 10,89 | 13,11 | 22,87 | 34,65 | 10,75 | 10,77 | 11,53 | 20,65 |
6 | 10,84 | 13,09 | 22,87 | 34,68 | 10,81 | 10,80 | 11,55 | 20,63 |
7 | 10,80 | 13,15 | 22,88 | 34,68 | 10,83 | 10,80 | 11,53 | 20,66 |
8 | 10,85 | 13,13 | 22,89 | 34,67 | 10,89 | 10,78 | 11,55 | 20,69 |
9 | 10,84 | 13,20 | 22,89 | 34,65 | 10,78 | 10,80 | 11,55 | 20,89 |
10 | 10,94 | 13,25 | 22,91 | 34,68 | 10,77 | 10,82 | 11,56 | 20,62 |
Average | 10,8716 | 13,189 | 22,893 | 34,7034 | 10,8056 | 10,7955 | 11,6317 | 20,6599 |
From this research we can get several conclusions:
- Dart is close to be as fast as JavaScript.
- The conversion from dart to JavaScript is close to 2 seconds slower than pure JavaScript.
- The JavaScript engine of Internet Explorer (version 11.0.96) is shit.
- Firefox doesn't like working with JavaScript generated by dart2js.
This are the conclusions I can get. This week I will do another research about how they manage the DOM and I will write a post like this to try to explain what I find out.
P.S.: The number of prime number between 0 and 250.000 are 22044.
No comments:
Post a Comment