Git Product home page Git Product logo

arrayhandle's Introduction

ArrayHandle

简单的数组map搞笑处理方法

如果固定查询数量,无论数组多少个元素,这种方法都能保证查询时间是常量的。

var _ = require("underscore")._;

var ah = new ArrayHandle();
// const __ = ah.ArrData.bind(ah);

const SIZE = 2000_0000;
x = 0;
let ary = _.range(SIZE);
console.time('t1');
targets = ah.ArrData(ary).map(v => {
    x++;
    return v + 1;
}).filter(v => {
    x++;
    return v % 2 == 0;
}).reverse().slice(1, 5).take(3).value();
console.timeEnd('t1');
console.log(targets, x, ' time');

基于上面的测试数据: 测试代码:

var ah = new ArrayHandle();
const __ = ah.ArrData.bind(ah);

const SIZE = 2000_0000;

ary = _.range(SIZE);

let x = 0;
console.time('origin_method');
targets = ary.map(v => {
    x++;
    return v + 1;
}).filter(v => {
    x++;
    return v % 2 == 0;
}).reverse().slice(1, 4);
console.timeEnd('origin_method');
console.log(targets, x, ' time');


ary = _.range(SIZE);

x = 0;
console.time('t1');
targets = __(ary).map(v => {
    x++;
    return v + 1;
}).filter(v => {
    x++;
    return v % 2 == 0;
}).reverse().slice(1, 5).take(3).value();
console.timeEnd('t1');
console.log(targets, x, ' time');

2000万个元素数据

$ node main.js 
origin_method: 1.020s
[ 19999998, 19999996, 19999994 ] 40000000  time
t1: 0.267ms
[ 19999998, 19999996, 19999994 ] 14  time

200万个元素数据

$ node main.js 
origin_method: 98.868ms
[ 1999998, 1999996, 1999994 ] 4000000  time
t1: 0.371ms
[ 1999998, 1999996, 1999994 ] 14  time

arrayhandle's People

Contributors

pjkui avatar

Watchers

 avatar

Forkers

sunnychenkang

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.