JavaScript Engine Performance Comparison of the two Google V8

Source: Internet
Author: User
Tags array sort assert readfile

Or the same JavaScript arraysort.js as the previous one, now performed by V8 to see how the test results

function Random_str () {    var text = "";    var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";    for (var i=0; i < 8; i++)        text + = Possible.charat (Math.floor (Math.random () * possible.length))    ; return text;} var array = new Array (), for (var i = 0; i < 10000; i++) {Array[array.length] = Random_str ();} Debug_trace ("Begin sort and reverse array which length=" + array.length); Array.Sort (); Array.reverse ();d ebug_trace (" Done, First element= "+ array[0]+", "+" last element= "+ array[array.length-1]);


Nonsense say, first on the source, basically according to the V8 bring the shell.cc rewrite and come


#include "include/v8.h" #include "include/libplatform/libplatform.h" #include "TinyUtil.h" using namespace v8;using  Namespace wfan;//Extracts A C string from a V8 utf8value.const char* tocstring (const v8::string::utf8value& value) { Return *value? *value: "<string conversion failed>";} void Debug_trace (const v8::functioncallbackinfo<v8::value>& args) {if (args).      Length () > 0) {char Sztimestr[time_fmt_len] = {'} '};        Current_timestamp (SZTIMESTR);      V8::string::utf8value str (args[0]);        Const char* CSTR = tocstring (str);  printf ("[%s]%s\n", Sztimestr, CStr);  }}v8::handle<v8::string> ReadFile (v8::isolate* Isolate, const char* name) {file* FILE = fopen (name, "RB");  if (file = = NULL) return v8::handle<v8::string> ();  fseek (file, 0, seek_end);  int size = Ftell (file);  Rewind (file);  char* chars = new Char[size + 1];  Chars[size] = ' + '; for (int i = 0; i < size;) {int read = static_cast<int> (Fread (&chars[i], 1, size-i, file);  i + = read;  } fclose (file);  v8::handle<v8::string> result = V8::string::newfromutf8 (isolate, chars, v8::string::knormalstring, size);  Delete[] chars; return result;}  V8::handle<v8::context> Createshellcontext (v8::isolate* Isolate) {//Create a template for the global object.  v8::handle<v8::objecttemplate> global = v8::objecttemplate::new (isolate);  Bind the global ' print ' function to the C + + print callback. Global->set (V8::string::newfromutf8 (Isolate, "Debug_trace"), V8::functiontemplate::new (Isolate, Debug_trac  e)); Return v8::context::new (Isolate, NULL, global);}  void Reportexception (v8::isolate* Isolate, v8::trycatch* try_catch) {v8::handlescope handle_scope (Isolate);  V8::string::utf8value exception (Try_catch->exception ());  Const char* exception_string = tocstring (Exception);  v8::handle<v8::message> Message = Try_catch->message (); if (message. IsEmpty ()) {//V8 didn ' t provide any extra inFormation about this error;    Just//Print the exception.  fprintf (stderr, "%s\n", exception_string);    else {//Print (filename):(line number): (message). V8::string::utf8value filename (Message->getscriptorigin ().    ResourceName ());    Const char* filename_string = tocstring (filename);    int linenum = Message->getlinenumber ();    fprintf (stderr, "%s:%i:%s\n", filename_string, LineNum, exception_string);    Print Line of source code.    V8::string::utf8value Sourceline (Message->getsourceline ());    Const char* sourceline_string = tocstring (sourceline);    fprintf (stderr, "%s\n", sourceline_string);    Print Wavy underline (Getunderline is deprecated).    int start = Message->getstartcolumn ();    for (int i = 0; i < start; i++) {fprintf (stderr, "");    } int end = Message->getendcolumn ();    for (int i = start; i < end; i++) {fprintf (stderr, "^");    } fprintf (stderr, "\ n"); V8::string::utf8value Stack_trace (try_catch->stackTrace ());      if (stack_trace.length () > 0) {const char* stack_trace_string = tocstring (stack_trace);    fprintf (stderr, "%s\n", stack_trace_string); }}}//executes a string within the current V8 Context.bool executestring (v8::isolate* Isolate, V8::han                   Dle<v8::string> Source, v8::handle<v8::value> name, BOOL Print_result,  BOOL report_exceptions) {V8::handlescope handle_scope (isolate);  V8::trycatch Try_catch;  V8::scriptorigin origin (name);  v8::handle<v8::script> Script = v8::script::compile (source, &origin); if (script.    IsEmpty ()) {//Print errors that happened during compilation.    if (report_exceptions) reportexception (Isolate, &try_catch);  return false;    } else {v8::handle<v8::value> result = Script->run (); if (result. IsEmpty ()) {assert (Try_catch.      Hascaught ());      Print errors that happened during execution. if (report_exceptions) reportexception (isolate, &try_catch);    return false; } else {assert (!try_catch.      Hascaught ()); if (Print_result &&!result->isundefined ()) {//If all went well and the result wasn ' t undefined then p        Rint//The returned value.        V8::string::utf8value str (result);        Const char* CSTR = tocstring (str);      printf ("%s\n", CStr);    } return true;  }}}int Main (int argc, char* argv[]) {//Initialize V8.  V8::initializeicu ();  platform* Platform = Platform::createdefaultplatform ();  V8::initializeplatform (platform);  V8::initialize ();  Create a new Isolate and make it the current one.  isolate* Isolate = Isolate::new ();    {Isolate::scope isolate_scope (Isolate);    Create a stack-allocated handle scope.    Handlescope Handle_scope (isolate);     Create a new context.     v8::handle<v8::context> Context = Createshellcontext (isolate); if (context. IsEmpty ()) {fprintf (stderr, "Error creating context\n ");     return 1;    }//Enter the context for compiling and running the "Hello World" script.    Context::scope Context_scope (context);    Create A string containing the JavaScript source code. Local<string> source;    local<string> filename;    if (argc > 1) {filename = String::newfromutf8 (isolate, argv[1]); source = ReadFile (Isolate, argv[1]); } else {Source = String::newfromutf8 (Isolate, "' Hello ' + ', world! '");    filename = String::newfromutf8 (Isolate, "nofile");  A long long begin_time = Current_timestamp (NULL);    Executestring (Isolate, Source,filename, false, true);    Long Long end_time = Current_timestamp (NULL);  printf ("Calling costs%lld microseconds\n", end_time-begin_time);  }//Dispose of the isolate and tear down V8.  Isolate->dispose ();  V8::D ispose ();  V8::shutdownplatform ();  Delete platform; return 0;}


The results of the control test are as follows

SpiderMonkey execution of this JS file cost a total of 46.781 MS, where 10,000 elements of a string array reversal took less than 3 ms time

$./test/spidermonkeytest./test/arraysort.js
[2015-05-10 08:22:20.246747] begin sort and reverse array which length=10000
[2015-05-10 08:22:20.249263] done, first element=zzvjwaqw, last Element=00bpapzs
Calling costs 46781 microseconds

V8 execution of this JS file cost a total of 22.326 MS, where 10,000 elements of the string array sort reversal took about 9 ms

$./test/v8test./test/arraysort.js
[2015-05-10 08:22:34.324314] begin sort and reverse array which length=10000
[2015-05-10 08:22:34.337588] done, first element=zzmblrhz, last ELEMENT=00G00SSM
Calling costs 22326 microseconds


Total time V8 is truly worthy of fame, the total execution is more than spidermonkey faster, but interestingly, in the internal string array of the time spent in the reversal of the SpiderMonkey rather far ahead, pending research

JavaScript Engine Performance Comparison of the two Google V8

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.