Optimize some code in index view and js