I heard a lot about how fast NodeJS is. But my simple test case shows, that Apache with PHP is much faster. The codes look like this:
PHP code
require_once("mysqlconnect.php");
$start = round(microtime(true) * 1000);
$r = mysql_query("SELECT field1 FROM mytable");
$arr = array();
while($s = mysql_fetch_array($r)){
$arr[] = $s;
}
$obj = json_encode($arr);
$end = round(microtime(true) * 1000);
echo $end-$start;
NodeJS
var mysql = require('mysql'),
start = new Date().getTime(),
connection = mysql.createConnection({
host : 'localhost',
user : 'root',
password : 'root',
database : 'testdb'
}),
json = '',
query = 'SELECT field1 FROM mytable';
connection.connect();
connection.query(query, function(err, results, fields) {
json = JSON.stringify(results);
var elapsed = new Date().getTime() - start;
console.log(elapsed);
connection.end();
});
The table contains 1000 rows and ten tests show that PHP even without any accelerators is at least 3 times faster. Why is that? Does it mean that NodeJS beats PHP only in some special cases?