1065 lines · 22.7 KB · JavaScript
RawDownload
/* * Javascript Micro benchmark * * Copyright (c) 2017-2019 Fabrice Bellard * Copyright (c) 2017-2019 Charlie Gordon * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */import * as std from "std"; function pad(str, n) {    str += "";    while (str.length < n)        str += " ";    return str;} function pad_left(str, n) {    str += "";    while (str.length < n)        str = " " + str;    return str;} function pad_center(str, n) {    str += "";    while (str.length < n) {        if ((n - str.length) & 1)            str = str + " ";        else            str = " " + str;    }    return str;} function toPrec(n, prec) {    var i, s;    for (i = 0; i < prec; i++)        n *= 10;    s = "" + Math.round(n);    for (i = s.length - prec; i <= 0; i++)        s = "0" + s;    if (prec > 0)        s = s.substring(0, i) + "." + s.substring(i);    return s;}                var ref_data;var log_data; var heads  = [ "TEST", "N", "TIME (ns)", "REF (ns)", "SCORE (%)" ];var widths = [    22,   10,          9,     9,       9 ];var precs  = [     0,   0,           2,     2,       2 ];var total  = [     0,   0,           0,     0,       0 ];var total_score = 0;var total_scale = 0; if (typeof console == "undefined") {    var console = { log: print };} function log_line() {    var i, n, s, a;    s = "";    for (i = 0, n = arguments.length; i < n; i++) {        if (i > 0)            s += " ";        a = arguments[i];        if (typeof a == "number") {            total[i] += a;            a = toPrec(a, precs[i]);            s += pad_left(a, widths[i]);        } else {            s += pad_left(a, widths[i]);        }    }    console.log(s);} var clocks_per_sec = 1000000;var max_iterations = 100;var clock_threshold = 2000;  /* favoring short measuring spans */var min_n_argument = 1;var get_clock; if (typeof globalThis.__date_clock != "function") {    console.log("using fallback millisecond clock");    clocks_per_sec = 1000;    max_iterations = 10;    clock_threshold = 100;    get_clock = Date.now;} else {    get_clock = globalThis.__date_clock;} function log_one(text, n, ti) {    var ref;     if (ref_data)        ref = ref_data[text];    else        ref = null;     ti = Math.round(ti * 100) / 100;    log_data[text] = ti;    if (typeof ref === "number") {        log_line(text, n, ti, ref, ti * 100 / ref);        total_score += ti * 100 / ref;        total_scale += 100;    } else {        log_line(text, n, ti);        total_score += 100;        total_scale += 100;    }} function bench(f, text){    var i, j, n, t, t1, ti, nb_its, ref, ti_n, ti_n1, min_ti;     nb_its = n = 1;    if (f.bench) {        ti_n = f(text);    } else {        ti_n = 1000000000;        min_ti = clock_threshold / 10;        for(i = 0; i < 30; i++) {            ti = 1000000000;            for (j = 0; j < max_iterations; j++) {                t = get_clock();                while ((t1 = get_clock()) == t)                    continue;                nb_its = f(n);                if (nb_its < 0)                    return; // test failure                t1 = get_clock() - t1;                if (ti > t1)                    ti = t1;            }            if (ti >= min_ti) {                ti_n1 = ti / nb_its;                if (ti_n > ti_n1)                    ti_n = ti_n1;            }            if (ti >= clock_threshold && n >= min_n_argument)                break;             n = n * [ 2, 2.5, 2 ][i % 3];        }        // to use only the best timing from the last loop, uncomment below        //ti_n = ti / nb_its;    }    /* nano seconds per iteration */    log_one(text, n, ti_n * 1e9 / clocks_per_sec);} var global_res; /* to be sure the code is not optimized */ function empty_loop(n) {    var j;    for(j = 0; j < n; j++) {    }    return n;} function date_now(n) {    var j;    for(j = 0; j < n; j++) {        Date.now();    }    return n;} function prop_read(n){    var obj, sum, j;    obj = {a: 1, b: 2, c:3, d:4 };    sum = 0;    for(j = 0; j < n; j++) {        sum += obj.a;        sum += obj.b;        sum += obj.c;        sum += obj.d;    }    global_res = sum;    return n * 4;} function prop_write(n){    var obj, j;    obj = {a: 1, b: 2, c:3, d:4 };    for(j = 0; j < n; j++) {        obj.a = j;        obj.b = j;        obj.c = j;        obj.d = j;    }    return n * 4;} function prop_create(n){    var obj, j;    for(j = 0; j < n; j++) {        obj = new Object();        obj.a = 1;        obj.b = 2;        obj.c = 3;        obj.d = 4;    }    return n * 4;} function prop_delete(n){    var obj, j;    obj = {};    for(j = 0; j < n; j++) {        obj[j] = 1;    }    for(j = 0; j < n; j++) {        delete obj[j];    }    return n;} function array_read(n){    var tab, len, sum, i, j;    tab = [];    len = 10;    for(i = 0; i < len; i++)        tab[i] = i;    sum = 0;    for(j = 0; j < n; j++) {        sum += tab[0];        sum += tab[1];        sum += tab[2];        sum += tab[3];        sum += tab[4];        sum += tab[5];        sum += tab[6];        sum += tab[7];        sum += tab[8];        sum += tab[9];    }    global_res = sum;    return len * n;} function array_write(n){    var tab, len, i, j;    tab = [];    len = 10;    for(i = 0; i < len; i++)        tab[i] = i;    for(j = 0; j < n; j++) {        tab[0] = j;        tab[1] = j;        tab[2] = j;        tab[3] = j;        tab[4] = j;        tab[5] = j;        tab[6] = j;        tab[7] = j;        tab[8] = j;        tab[9] = j;    }    return len * n;} function array_prop_create(n){    var tab, i, j, len;    len = 1000;    for(j = 0; j < n; j++) {        tab = [];        for(i = 0; i < len; i++)            tab[i] = i;    }    return len * n;} function array_length_decr(n){    var tab, i, j, len;    len = 1000;    tab = [];    for(i = 0; i < len; i++)        tab[i] = i;    for(j = 0; j < n; j++) {        for(i = len - 1; i >= 0; i--)            tab.length = i;    }    return len * n;} function array_hole_length_decr(n){    var tab, i, j, len;    len = 1000;    tab = [];    for(i = 0; i < len; i++) {        if (i != 3)            tab[i] = i;    }    for(j = 0; j < n; j++) {        for(i = len - 1; i >= 0; i--)            tab.length = i;    }    return len * n;} function array_push(n){    var tab, i, j, len;    len = 500;    for(j = 0; j < n; j++) {        tab = [];        for(i = 0; i < len; i++)            tab.push(i);    }    return len * n;} function array_pop(n){    var tab, i, j, len, sum;    len = 500;    for(j = 0; j < n; j++) {        tab = [];        for(i = 0; i < len; i++)            tab[i] = i;        sum = 0;        for(i = 0; i < len; i++)            sum += tab.pop();        global_res = sum;    }    return len * n;} function typed_array_read(n){    var tab, len, sum, i, j;    len = 10;    tab = new Int32Array(len);    for(i = 0; i < len; i++)        tab[i] = i;    sum = 0;    for(j = 0; j < n; j++) {        sum += tab[0];        sum += tab[1];        sum += tab[2];        sum += tab[3];        sum += tab[4];        sum += tab[5];        sum += tab[6];        sum += tab[7];        sum += tab[8];        sum += tab[9];    }    global_res = sum;    return len * n;} function typed_array_write(n){    var tab, len, i, j;    len = 10;    tab = new Int32Array(len);    for(i = 0; i < len; i++)        tab[i] = i;    for(j = 0; j < n; j++) {        tab[0] = j;        tab[1] = j;        tab[2] = j;        tab[3] = j;        tab[4] = j;        tab[5] = j;        tab[6] = j;        tab[7] = j;        tab[8] = j;        tab[9] = j;    }    return len * n;} var global_var0; function global_read(n){    var sum, j;    global_var0 = 0;    sum = 0;    for(j = 0; j < n; j++) {        sum += global_var0;        sum += global_var0;        sum += global_var0;        sum += global_var0;    }    global_res = sum;    return n * 4;} var global_write =    (1, eval)(`(function global_write(n)           {               var j;               for(j = 0; j < n; j++) {                   global_var0 = j;                   global_var0 = j;                   global_var0 = j;                   global_var0 = j;               }               return n * 4;           })`); function global_write_strict(n){    var j;    for(j = 0; j < n; j++) {        global_var0 = j;        global_var0 = j;        global_var0 = j;        global_var0 = j;    }    return n * 4;} function local_destruct(n){    var j, v1, v2, v3, v4;    var array = [ 1, 2, 3, 4, 5];    var o = { a:1, b:2, c:3, d:4 };    var a, b, c, d;    for(j = 0; j < n; j++) {        [ v1, v2,, v3, ...v4] = array;        ({ a, b, c, d } = o);        ({ a: a, b: b, c: c, d: d } = o);    }    return n * 12;} var global_v1, global_v2, global_v3, global_v4;var global_a, global_b, global_c, global_d; var global_destruct =    (1, eval)(`(function global_destruct(n)           {               var j, v1, v2, v3, v4;               var array = [ 1, 2, 3, 4, 5 ];               var o = { a:1, b:2, c:3, d:4 };               var a, b, c, d;               for(j = 0; j < n; j++) {                   [ global_v1, global_v2,, global_v3, ...global_v4] = array;                   ({ a: global_a, b: global_b, c: global_c, d: global_d } = o);               }               return n * 8;          })`); function global_destruct_strict(n){    var j, v1, v2, v3, v4;    var array = [ 1, 2, 3, 4, 5 ];    var o = { a:1, b:2, c:3, d:4 };    var a, b, c, d;    for(j = 0; j < n; j++) {        [ global_v1, global_v2,, global_v3, ...global_v4] = array;        ({ a: global_a, b: global_b, c: global_c, d: global_d } = o);    }    return n * 8;} function func_call(n){    function f(a)    {        return 1;    }     var j, sum;    sum = 0;

Lines 1–500 of 1065.

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t