Extreme Battle Lag

So I'am using a script in my game that adds animated face sprites
it works great but in battles the game lags so much when there's more
than 1 character with an animated face. Also when you use an item the game
lags big time and it lasts through the entire battle. The item thing even
effects when there's only one party member. The game drops from 60 FPS down to 9 FPS in seconds

Can someone please fix this script?
I really need some help.
Thanks!
Code:
#===============================================================================
# )----------------------------------------------------------------------------(
# )--     AUTHOR:     Mr Trivel                                              --(
# )--     NAME:       Animated Faces                                         --(
# )--     CREATED:    2014-09-12                                             --(
# )--     VERSION:    1.2                                                    --(
#===============================================================================
# )----------------------------------------------------------------------------(
# )--                         VERSION HISTORY                                --(
# )--  1.0 - Initial script.                                                 --(
# )--  1.1 - Fixed bugs. Uses Frames per second instead of pure frames.      --(
# )--  1.2 - Fixed more bugs. Loops through drawn faces instead of party.    --(
#===============================================================================
# )----------------------------------------------------------------------------(
# )--                          DESCRIPTION                                   --(
# )--  Allows for actor faces to be animated.                                --(
#===============================================================================
# )----------------------------------------------------------------------------(
# )--                          INSTRUCTIONS                                  --(
# )--  Define animation files and frame sequence in ANIMATED_FACES module.   --(
#===============================================================================
# )----------------------------------------------------------------------------(
# )--                          LICENSE INFO                                  --(
# )--  Free for non-commercial & commercial games if credit was given to     --(
# )--  Mr Trivel.                                                            --(
# )----------------------------------------------------------------------------(
#===============================================================================

# )=======---------------------------------------------------------------------(
# )-- Module: ANIMATED_FACES                                                 --(
# )---------------------------------------------------------------------=======(
module ANIMATED_FACES
  # )--------------------------------------------------------------------------(
  # )-- Place your animated faces data here.                                 --(
  # )--------------------------------------------------------------------------(
  DATA = {
  # )-- ACTOR_ID    Filename      Frame Sequence                 
          1 => [ "Casey_Face.png", [0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 2, 3, 4, 5, 6, 7, 7, 7, 7, 7, 7, 6 ,5 ,4, 3, 2, 1, 1, 1, 1, 1,1] ],
          2 => [ "Actor2.png", [0, 1, 2, 3, 2, 0, 0, 0, 0, 0 ,0, 0] ]
 
  }
 
  # )--------------------------------------------------------------------------(
  # )-- How fast should frames change. Speed in milliseconds.                --(
  # )--------------------------------------------------------------------------(
  FRAME_SPEED = 20
end

# )=======---------------------------------------------------------------------(
# )-- class: Window_Base                                                     --(
# )---------------------------------------------------------------------=======(
class Window_Base < Window
  include ANIMATED_FACES

  alias :mrts_face_anims_update :update
  alias :mrts_face_anims_initialize :initialize
 
  # )--------------------------------------------------------------------------(
  # )-- Alias: initialize                                                    --(
  # )--------------------------------------------------------------------------(
  def initialize(x, y, width, height)
    mrts_face_anims_initialize(x, y, width, height)
    @time_now = Time.now
    @elapsed_time = 0.0
    @actor_hash = {}
    
  end
  # )--------------------------------------------------------------------------(
  # )-- Overwrite: draw_actor_face                                           --(
  # )--------------------------------------------------------------------------(
  def draw_actor_face(actor, x, y, enabled = true)
    data = DATA[actor.id]
    if data
      @animated_actors ||= true
      @actor_hash[actor.id] ||= 0
      face_name = data[0]
      face_index = data[1][@actor_hash[actor.id]]
      draw_face(face_name, face_index, x, y, enabled)
    else
      draw_face(actor.face_name, actor.face_index, x, y, enabled)
    end
  end
 
  # )--------------------------------------------------------------------------(
  # )-- Alias: update                                                        --(
  # )--------------------------------------------------------------------------(
  def update   
    finish = Time.now
    @elapsed_time += ms(@time_now, finish)
    mrts_face_anims_update
    if @animated_actors && @elapsed_time >= FRAME_SPEED && Time.now.to_f != @time_now
      @actor_hash.each { |k, v| 
        next unless @actor_hash[k]
        @actor_hash[k] += 1
        @actor_hash[k] = 0 if @actor_hash[k] > DATA[k][1].size-1
      }
      refresh
      @elapsed_time = 0
    end
    @time_now = Time.now
  end
 
  # )--------------------------------------------------------------------------(
  # )-- New Method: ms                                                       --(
  # )--------------------------------------------------------------------------(
  def ms(start, finish)
   (finish - start) * 1000.0
  end
end
you're hooking up the validation loop from the Window_Base update, which effectively runs for every single window.
you're hooking up the validation loop from the Window_Base update, which effectively runs for every single window.
I honestly don't understand is there a way I can fix this?
I honestly don't understand is there a way I can fix this?

yes, it's just a misplaced procedure.
I can show you how, but right now I don't have a clean Ace project at hand.... give me a minute.

EDIT:
here.
Spoiler
Code:
module ANIMATED_FACES
  DATA = {
          1 => [ "Actor1.png", [0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 2, 3, 4, 5, 6, 7, 7, 7, 7, 7, 7, 6 ,5 ,4, 3, 2, 1, 1, 1, 1, 1,1] ],
          2 => [ "Actor2.png", [0, 1, 2, 3, 2, 0, 0, 0, 0, 0 ,0, 0] ]

  }
  FRAME_SPEED = 20
end

class Window_Base < Window
  include ANIMATED_FACES

  alias :mrts_face_anims_initialize :initialize 
  def initialize(x, y, width, height)
    mrts_face_anims_initialize(x, y, width, height)
    @time_now = Time.now
    @elapsed_time = 0.0
    @actor_hash = {}    
  end
 
  def draw_actor_face(actor, x, y, enabled = true)
    data = DATA[actor.id]
    if data
      @animated_actors ||= true
      @actor_hash[actor.id] ||= 0
      face_name = data[0]
      face_index = data[1][@actor_hash[actor.id]]
      draw_face(face_name, face_index, x, y, enabled)
    else
      draw_face(actor.face_name, actor.face_index, x, y, enabled)
    end
  end

  def ms(start, finish)
   (finish - start) * 1000.0
  end

  def update_anim_frame
    finish = Time.now
    @elapsed_time += ms(@time_now, finish)
    if @animated_actors && @elapsed_time >= FRAME_SPEED && Time.now.to_f != @time_now
      @actor_hash.each { |k, v| 
        next unless @actor_hash[k]
        @actor_hash[k] += 1
        @actor_hash[k] = 0 if @actor_hash[k] > DATA[k][1].size-1
      }
      refresh
      @elapsed_time = 0
    end
    @time_now = Time.now
  end
end


class Window_MenuStatus < Window_Selectable
  def update
    super
    update_anim_frame
  end
end

class Window_SkillStatus < Window_Base
  def update
    super
    update_anim_frame
  end
end

class Window_Status < Window_Selectable
  def update
    super
    update_anim_frame
  end
end

basically what I did was split the new code away from the update procedure, and make it into a separate procedure called update_anim_frame.
then, have all windows relying on draw_actor_face() call update_anim_frame after calling their parent's update.
that way, no procedure that doesn't require draw_actor_face() will run that extra chunk of code.

I'm sure it can be made more efficiently, but I don't want to think right now 🙁
yes, it's just a misplaced procedure.
I can show you how, but right now I don't have a clean Ace project at hand.... give me a minute.

EDIT:
here.
Spoiler
Code:
module ANIMATED_FACES
  DATA = {
          1 => [ "Actor1.png", [0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 2, 3, 4, 5, 6, 7, 7, 7, 7, 7, 7, 6 ,5 ,4, 3, 2, 1, 1, 1, 1, 1,1] ],
          2 => [ "Actor2.png", [0, 1, 2, 3, 2, 0, 0, 0, 0, 0 ,0, 0] ]

  }
  FRAME_SPEED = 20
end

class Window_Base < Window
  include ANIMATED_FACES

  alias :mrts_face_anims_initialize :initialize
  def initialize(x, y, width, height)
    mrts_face_anims_initialize(x, y, width, height)
    @time_now = Time.now
    @elapsed_time = 0.0
    @actor_hash = {}   
  end
 
  def draw_actor_face(actor, x, y, enabled = true)
    data = DATA[actor.id]
    if data
      @animated_actors ||= true
      @actor_hash[actor.id] ||= 0
      face_name = data[0]
      face_index = data[1][@actor_hash[actor.id]]
      draw_face(face_name, face_index, x, y, enabled)
    else
      draw_face(actor.face_name, actor.face_index, x, y, enabled)
    end
  end

  def ms(start, finish)
   (finish - start) * 1000.0
  end

  def update_anim_frame
    finish = Time.now
    @elapsed_time += ms(@time_now, finish)
    if @animated_actors && @elapsed_time >= FRAME_SPEED && Time.now.to_f != @time_now
      @actor_hash.each { |k, v|
        next unless @actor_hash[k]
        @actor_hash[k] += 1
        @actor_hash[k] = 0 if @actor_hash[k] > DATA[k][1].size-1
      }
      refresh
      @elapsed_time = 0
    end
    @time_now = Time.now
  end
end


class Window_MenuStatus < Window_Selectable
  def update
    super
    update_anim_frame
  end
end

class Window_SkillStatus < Window_Base
  def update
    super
    update_anim_frame
  end
end

class Window_Status < Window_Selectable
  def update
    super
    update_anim_frame
  end
end

basically what I did was split the new code away from the update procedure, and make it into a separate procedure called update_anim_frame.
then, have all windows relying on draw_actor_face() call update_anim_frame after calling their parent's update.
that way, no procedure that doesn't require draw_actor_face() will run that extra chunk of code.

I'm sure it can be made more efficiently, but I don't want to think right now 🙁
Thanks for taking the time to help me!!!
It means alot!

Original thread: https://forums.rpgmakerweb.com/threads/extreme-battle-lag.105080/