小编Ber*_*rgi的帖子

JS 性能:使用对象字面量进行函数调用 VS 存储在变量中的对象

在我的程序中,我通常有很多带有签名的函数myFunc({ param })。我想测试每次使用新对象或使用包含对象的变量调用这些函数之间有什么区别。IE:

myFuncA({ param: 1 });

// vs

const paramObj = { param: 1 };
myFuncB(paramObj);
Run Code Online (Sandbox Code Playgroud)

所以我想出了一个简单的测试:

let a = 0;
let b = 0;

function myFuncA({ param }) {
    a += param;
}

function myFuncB({ param }) {
    b += param;
}

const iterations = 1e9;

console.time('myFuncA');
for(let i = 0; i < iterations; i++) {
    myFuncA({ param: 1 });
}
console.timeEnd('myFuncA')

console.time('myFuncB');
const paramObj = { param: 1 };
for(let i = 0; i < …
Run Code Online (Sandbox Code Playgroud)

javascript performance v8 object-literal node.js

1
推荐指数
1
解决办法
564
查看次数

截断 Postgres 中除列表中提供的表之外的所有表

我想截断整个数据库,同时保持序列同一性。我想出了这样的事情:

WITH tables_to_be_truncated AS (
SELECT table_name
    FROM information_schema.tables
    WHERE table_type='BASE TABLE'
    AND table_schema='public'
    AND table_name NOT IN ('admins', 'admin_roles')
)
TRUNCATE TABLE (SELECT table_name FROM tables_to_be_truncated) CONTINUE IDENTITY RESTRICT;
Run Code Online (Sandbox Code Playgroud)

我收到此错误:

ERROR:  syntax error at or near "TRUNCATE"
LINE 9: TRUNCATE TABLE (SELECT table_name FROM tables_to_be_truncated...
Run Code Online (Sandbox Code Playgroud)

我确实有权截断表,并且当我运行单个表时,TRUNCATE TABLE access_tokens它工作得很好。

我也尝试过这个

ERROR:  syntax error at or near "TRUNCATE"
LINE 9: TRUNCATE TABLE (SELECT table_name FROM tables_to_be_truncated...
Run Code Online (Sandbox Code Playgroud)

但效果不太好。

从我在其他帖子中看到的情况来看,人们正在用函数来做这件事。老实说,我不想走这条路,但如果这是唯一的方法......

postgresql truncate dynamic-sql

1
推荐指数
1
解决办法
3411
查看次数

异步生成器在解决时产生承诺结果

假设我想同时获取 10 个 url,并在收到响应时对其进行处理(其顺序可能与它们在原始列表中出现的顺序不同)。忽略拒绝的可能性,一种方法是简单地为每个 Promise 附加一个“then”回调,然后等待它们全部使用完成Promise.all()

const fetch_promises = [
  fetch("https://cors-demo.glitch.me/allow-cors"),
  fetch("/"),
  fetch("."),
  fetch(""),
  fetch("https://enable-cors.org"),
  fetch("https://html5rocks-cors.s3-website-us-east-1.amazonaws.com/index.html"),
  fetch("https://api.github.com"),
  fetch("https://api.flickr.com/services/rest/"),
];
const processing_promises = [];
for (const fetch_promise of fetch_promises) {
  processing_promises.push(fetch_promise.then(response => {
    // Process response.  In this example, that means just
    // print it.
    console.log("got a response: ",response);
  }));
}
await Promise.all(processing_promises);
Run Code Online (Sandbox Code Playgroud)

切换到输出更清晰、更具确定性的示例:

const sleep = millis => new Promise(resolve=>setTimeout(resolve, millis));
const sleep_promises = [
    sleep(3000).then(()=>"slept 3000"),
    sleep(1000).then(()=>"slept 1000"),
    sleep(5000).then(()=>"slept 5000"),
    sleep(4000).then(()=>"slept 4000"),
    sleep(2000).then(()=>"slept 2000"),
];
const processing_promises …
Run Code Online (Sandbox Code Playgroud)

javascript generator fetch promise async-await

1
推荐指数
1
解决办法
1463
查看次数

找到json字符串的长度

我有以下Jsonstring

  var j = { "name": "John" };
            alert(j.length);
Run Code Online (Sandbox Code Playgroud)

它警告:undefined,我怎样才能找到json Array对象的长度?

谢谢

javascript json

0
推荐指数
1
解决办法
2万
查看次数

如何将Dictionary <string,object>序列化为javascript?

这是我的代码:

public class PuntoMappa
{
    string Lat;
    string Lng;

    public PuntoMappa(string Lat, string Lng)
    {
        this.Lat = Lat;
        this.Lng = Lng;
    }
}   

PuntiCategoriaMappa.Add("1111", new PuntoMappa("1", "2"));
PuntiCategoriaMappa.Add("2222", new PuntoMappa("3", "4"));
PuntiCategoriaMappa.Add("3333", new PuntoMappa("5", "6"));

var jsonSerializer = new System.Web.Script.Serialization.JavaScriptSerializer();
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "PuntiCategoriaMappa", "PuntiCategoriaMappa = " + jsonSerializer.Serialize(PuntiCategoriaMappa) + ";", true); 
Run Code Online (Sandbox Code Playgroud)

但序列化是:

PuntiCategoriaMappa = {"1111":{},"2222":{},"3333":{}};
Run Code Online (Sandbox Code Playgroud)

好吧,我丢失了PuntoMappa对象的序列化.

我怎样才能正确地做到这一点?

c# serialization json dictionary

0
推荐指数
1
解决办法
579
查看次数

不知道为什么这不起作用 - Javascript基本增量功能

我正在编写代码来增加最基本的进度条......它只是不起作用.这些是使用的变量:

map.progress_bar = // the progress div that grows inside a container div that is the width of the screen (100%);

progress = 0;

state.window_width = // the width of the window or otherwise $(window).width();

setTimeout(function incProgress(){
        if ( map.progress_bar.width() < state.window_width ) {
            progress += 10;
            map.progress_bar.css({
                width: String(progress + '%')
            });
            console.log('progress: ', map.progress_bar.width());
            console.log('window: ', state.window_width);
            setTimeout(incProgress(), 300);
        }
    }, 300);
Run Code Online (Sandbox Code Playgroud)

请不要让我做setInterval.请向我解释为什么在地球上这不起作用,我感到非常不开心.

javascript

0
推荐指数
1
解决办法
38
查看次数

由于URI名称中的空格,节点的exec()无法正常工作

var exec = require('child_process').exec
var cmd = 'C:\\Users\\Johnny Cash\\Desktop\\executeme.exe'

exec(cmd, function(e, stdout, stderr) {
  console.log(e);
  console.log(stdout);
  console.log(stderr);
});
Run Code Online (Sandbox Code Playgroud)

"C:\ Users\Johnny"未被识别为内部或外部命令

这必须是有史以来最新的问题,但如何在窗户上使用空格来逃避这些路径呢?它在这个空间被切断了,我所做的一切(单人或双人事先逃脱)似乎都可以解决问题.是否exec()做了一些格式,我不知道的?

javascript windows escaping path node.js

0
推荐指数
1
解决办法
2134
查看次数

Promise.join不使用箭头功能

我一直在处理一个函数,它返回几个数据库表的所有值.我正在尝试主动采用稳定的es6功能,这似乎是箭头功能的好机会.我使用蓝鸟作为承诺和续集作为我的ORM(它返回蓝鸟承诺的数据库查询)

/**
 * This does not work on node v4.2.0
 */
'use strict';

const Promise = require('bluebird');
const models = require('../database');

const resource = {
    browse: function browse() {
        return Promise.join(
            () => models.table_one.findAll(),
            () => models.table_two.findAll(),
            () => models.table_three.findAll(),
            function(table_one, table_two, table_three) {
                const response = {
                    table_one,
                    table_two,
                    table_three,
                };

                return response;
            });
    },
};

module.exports = resource;
Run Code Online (Sandbox Code Playgroud)

这不起作用

/**
 * But this does
 */
'use strict';

const Promise = require('bluebird');
const models = require('../database');

const resource = …
Run Code Online (Sandbox Code Playgroud)

javascript node.js bluebird

0
推荐指数
1
解决办法
313
查看次数

用取消方法增加ES6承诺

我正在尝试编写一些代码,这些代码在启动一些可能长时间运行的异步活动后返回ES6承诺.但是,我希望有可能取消该活动,所以我想用'取消'方法来增加我的承诺.

一个sscce说明了我想要做的是如下:

function TimerPromise(timeInterval) {
    var timer;

    var p = new Promise(
        function(resolve,reject) {
            timer = setTimeout(
                function() {
                    resolve(true);
                },
                timeInterval
            );
        }
    );

    p.cancel = function() {
        clearTimeout(timer);
    };

    console.log("p.cancel is ",p.cancel);

    return p;
}

var t = TimerPromise(2000).then(function(res) { console.log("Result is ",res); });

t.cancel();
Run Code Online (Sandbox Code Playgroud)

在示例中,TimerPromise只设置一个计时器来模拟长时间运行的异步活动.

这是我在运行时得到的:

$ node test.js
p.cancel is  function () {
                timer.clearTimeout();
        }
/home/harmic/js/src/test.js:28
t.cancel();
  ^

TypeError: t.cancel is not a function
    at Object.<anonymous> (/home/harmic/js/src/test.js:28:3)
    at Module._compile (module.js:413:34)
    at Object.Module._extensions..js (module.js:422:10)
    at …
Run Code Online (Sandbox Code Playgroud)

javascript node.js promise cancellation es6-promise

0
推荐指数
1
解决办法
399
查看次数

什么:在jquery中意味着什么?

我遇到了一些具有以下内容的代码:

var game = {
    gameBoard: [0, 0, 0,
                   0, 0, 0,
                   0, 0, 0],
    user: 'X',
    yOn: '',
    newStep: function(){
        ...
    }
}
Run Code Online (Sandbox Code Playgroud)

我很困惑,因为我从未见过:像之前那样使用过.我复制了代码并尝试通过添加=来改变它,无论哪里有一个:但我一直得到一个意外的标识符错误(我这样做是为了看看:是否与=相同,我猜不是).

javascript object-literal colon

0
推荐指数
1
解决办法
66
查看次数