Информация об изменениях

Сообщение Re[18]: Реальная производительность WebAssembly? от 18.09.2017 22:43

Изменено 18.09.2017 23:34 alexzzzz

Re[18]: Реальная производительность WebAssembly?
Здравствуйте, alex_public, Вы писали:

_>

_>
  JS
_>
_>function CreateId()
_>{
_>    return Math.floor(Math.random()* Math.pow(10, 8)).toString().padStart(10, '0');
_>}

_>class TestClass
_>{
_>    constructor() {
_>        this.Id = CreateId();
_>        this.Value = CreateId();
_>    }
_>}
        
_>var startInit = performance.now();

_>var vals = [];
_>for (var i = 0; i < 1000 * 1000; i++)
_>{
_>    vals.push(new TestClass());
_>}

_>var endInit = performance.now();
_>log("Init: " + (endInit - startInit) + " msecs");
_>


Как это запустить? Я не секу в JS. Скачал поставил node.js, а оно ругается:

var startInit = performance.now();
                            ^

ReferenceError: performance is not defined
    at Object.<anonymous> (H:\Users\Alex\Desktop\test.js:14:29)
    at Module._compile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Module.runMain (module.js:604:10)
    at run (bootstrap_node.js:389:7)
    at startup (bootstrap_node.js:149:9)
    at bootstrap_node.js:502:3


Очевидно, надо этот performance как-то импортировать.

PS
Поправил так:
  JS
var now = require("performance-now")
var padStart = require("pad-start")
var log = require("log")

function CreateId()
{
    return padStart(Math.floor(Math.random() * Math.pow(10, 8)).toString(), 10, '0');
}

class TestClass
{
    constructor() {
        this.Id = CreateId();
        this.Value = CreateId();
    }
}
        
var startInit = now();

var vals = [];
for (var i = 0; i < 1000 * 1000; i++)
{
    vals.push(new TestClass());
}

var endInit = now();
log("Init: " + (endInit - startInit) + " msecs");

Но оно ничего не печатает. Ошибок больше не выдаёт, но если и работает, в консоли ничего не появляется.

PPS
Разобрался. Рабочий вариант:

  JS
var now = require("performance-now")
var padStart = require("pad-start")

function CreateId()
{
    return padStart(Math.floor(Math.random() * Math.pow(10, 8)).toString(), 10, '0');
}

class TestClass
{
    constructor() {
        this.Id = CreateId();
        this.Value = CreateId();
    }
}
        
var startInit = now();

var vals = [];
for (var i = 0; i < 1000 * 1000; i++)
{
    vals.push(new TestClass());
}

var endInit = now();
console.log("Init: " + (endInit - startInit) + " msecs");
Re[18]: Реальная производительность WebAssembly?
[del]