Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

This is part of a JavaScript web application I made as a personal project to browse a 10k movie database. (available here)

The application is loading a big CSV file (5Mb) and then making some plots using dc.js and a table with datatables JavaScript plugin.
The following code is too slow (>15 secs.) to be practical to use.
It's the CSV loading d3.csv() that is the slowest.

I'm looking for any idea to make things load faster like asynchronous loading or other methods. (only hints or ideas are also very welcome)

Many thanks!

// CSV FILE LOADING ~15 secs.
//=============================================================
var filePath= "data/Movies10k-V1.2.csv";
d3.csv(filePath, function(error, data_csv) {
    if (error) {
        console.log('Error loading CSV');
        console.log(error);
    }
    else {
        createGraphs(data_csv);
    }
});


function createGraphs(data_movies){

    // some recodings and calculations
    //=============================================================
    data_movies.forEach(function (d, i) {
        // properties list: ID,Title,Year,Rating,NumVotes,imdbID,Type,Released,Runtime,Genres,Directors,Actors,Plot,Country,Metascore,imdbRating,imdbVotes,tomatoMeter,tomatoReviews,tomatoUserMeter,tomatoUserReviews,DVD,poster_path,release_date

        d.ID = +d.ID;
        d.Year = +d.Year;

        (...)
    });

    // crossfilter dimensions and groups
    //=============================================================
    var ndx = crossfilter(data_movies)
        ,all = ndx.groupAll()

        ,YearDimension = ndx.dimension(function(d) { if (d.DateRelease != null) {return d3.time.year(d.DateRelease)  ;}/*else {return 2015;}*/ })
        ,YearGroup = YearDimension.group()

        ,GlobalScoreDimension = ndx.dimension(function(d) { if ( !isNaN(d.GlobalScore) ) {return Math.round(d.GlobalScore*10)/10 ;} else {return -1;} })
        ,GlobalScoreGroup = GlobalScoreDimension.group()

        (...)

    ;

    // CHARTS
    //=============================================================
    chart_year
        .width(width001)
        .height(height001)
        .margins(margins)
        .dimension(YearDimension)
        .group(YearGroup)
        .x(d3.time.scale().domain(
            [MinDateRelease.setMonth(MinDateRelease.getMonth()-12), MaxDateRelease.setMonth(MaxDateRelease.getMonth()+12)]
            )
        )
        .round(d3.time.year.round)
        .renderArea(true)
        .transitionDuration(transitionDuration)
        .elasticY(true)
        .renderHorizontalGridLines(true)
        .brushOn(true)
        .mouseZoomable(false)
        .yAxis().ticks(2)
        ;
    (...)

    // DATATABLE
    //=============================================================
    $('#dc-data-table').html('  <thead> <tr> <th>Poster</th> <th>Title</th> <th>Year</th> <th>Scores</th> <th>Votes</th> <th>Runtime</th> <th>Genres</th> <th></th> </tr> </thead>');

    var dataTableOptions = {
        "lengthMenu": [ [15, 50, 200, 5], [15, 50, 200, 5] ]
        ,"order": [[1, 'desc']]

        ,columnDefs: [
            { targets: 0,
                data: function (d) {
                    return 'col1';
                }}
            ,{ targets: 1,
                data: function (d) {
                    return 'col2';
                }}
            (...)
        ]
    };

    (...)

    var datatable = $('#dc-data-table').dataTable(dataTableOptions) ;
    dc.renderAll();
}
share|improve this question

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.